Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding number of ways a piece on a chess board can move with number and type of moves given
    primarykey
    data
    text
    <p>This is the dynamic programming problem I encountered on a site. I solved the problem with the algorithm mentioned below. Though the answers I got were right, the evaluation shows that the Time limit exceeds.</p> <p>The problem is, on an N*N board, a piece is at a location (xp,yp).It can move to (xd,yd) if and only if abs(xd-xp)+abs(yd-yp) &lt;= S. The piece has to make M moves. The problem gives N,S, M and (xp,yp) and I have to find how many ways the piece can make M moves on this board. </p> <p>My solution was as follows. </p> <pre><code>Matrix m0 is initialized with all 0s except with 1 in the location (xp,yp) Make a N*N matrix m1 for one move from an initialized matrix m0 Make a N*N matrix m2 for two moves from matrix m1 ... carry on till m moves and the sum of elements of the final matrix mm gives me the number of ways the piece can make M moves on the N*N board with given distance constraint S. </code></pre> <p>My pseudocode for calculating a matrix is as follows</p> <pre><code>for each element (e) in matrix m, consider the elements (es) in S*S (S is the distance constraint given in the problem) box surrounding the elements. If (abs(es.x-e.x) + abs(es.y-e.y)) &lt;= S, then m[e.x][e.y] = m[e.x][e.y] + m[es.x][es.y]. (adding the # of ways that es can be reached with number of ways that e can be reached so that position of e in the matrix contains # of ways that e can be reached from es) In the S*S box, I considered all elements except the current element e. </code></pre> <p>As per my understanding, the above solution is (N^2)*(S^2) and runs slow though it gives the correct answer. Please suggest an idea of implementing this in N^2 time. Since it involves a 2D board, I presume it can't be done in O(N) time, but I may be wrong.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload