Hi @amandahiya.3572
The problem is that you are not using backtracking and considering the mixture of all possible moves of the knights all together …we have to consider each path of it and count of the number of cells visited in that path and minimise that number. Your code simply counts the number of all the possible cells that the knight can reach in total.
That is ,you have find one path where the knight can cover the maximum no of cells and your knight has to stick to it without going back.
Here’s a testcase for you to understand
Input :
4
1 0 1 0
0 0 1 0
1 1 0 1
0 0 0 0
Expected Output : 2
Your Output : 0
Explanation : There are only two possible paths for the knight to traverse without going back .
( I am using 0 based indexing here )
Path 1 : (0,0) -> (2,1) -> (0,2) -> (2,3)
Path 2 : (0,0) -> (1,2) -> (2,0)
Clearly we would prefer Path 1 as it covers more nodes.
The no of nodes left out in path 1 were 2 and nodes left out in path 2 were 3.
Final answer will be 2 as Path 1 is the better one.
Hope this helps.
Here’s another testcase for you to try when you update your code.
Input :
10
1 1 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 1 1 1 1 1 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 1 1 0
0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1
Expected Output : 17