test cases not run…
Funky chess board
Hello @dakshi,
Overall logic of your code is correct.
But the way you are using backtracking will result into wrong output.
Correct approach:
Hope, this would help.
Give a like if you are satisfied.
plz point the misktake
i cannot get it …
why the maximum is taken ?
why reintialising the cell with 1…
Hello @dakshi,
To understand the answer to your question, you are required to understand the logic of that code.
LOGIC:
- Starting from the (0,0) position, we are going to move to all the possible locations that are possible to visit.
- You mark that position as 0 because as you are at that location. So, now you cannot go to it again.
- When you are standing at (0,0) position, you can move 8 different moves: (r-2,c-1), (r-2,c+1), (r-1,c-2), (r-1,c+2), (r+1,c-2), (r+1,c+2), (r+2,c-1), or (r+2,c+1).
- You will select a move out of 8 possible moves which will allow you to iterate to maximum location.
Reason:
As you can choose only one move from a particular index. So, choosing the move that will give maximum value is optimal. - Suppose, you first go to (r-2,c-1) you will mark this position as 0 in the recursive call.
But, when you will return from it. Before returning, you will mark it again 1.
As, thereafter you will go for 7 remaining moves. So, all the positions that we ca reach from (r-2,c-1) can also be reached from any of the other seven moves.
Thus, if you want make them 1 again. Then you will not get your answer.
1 Like