why code is working only for one test case
Funky chess board
Hello @dakshi,
You should try the code the other way.
Find all the 1’s initially in the matrix and then use the same function with lil modification to find the maximum numbers of cells you can visit.
Later, simply subtract them to get the required answer.
Or if you want i can look into the same approach you have written.
Hello @S18ML0016,
There is a problem in this approach.
Example:
3
1 0 0
0 0 1
0 1 0
Expected Output:
0
Your Output:
1
Reason:
When you are at (0,0) position, you can either go to (1,2) or (2,1). But not both. Thus, 1 place will remain out of reach.
Why are getting wrong output with your code?
- When you are at (0,0), you marked the dp[0][0]=1
- Then you went to (1,2) and marked the dp[1][2]=1
- Then you returned from that call and went to (2,1) and marked dp[2][1]=1
Thus, your code shows 0 as output.
Suggestion:
Try to understand the solution i have shared in the another post.