Why are we checking for this -
if(grid[0][1] == ‘#’ || grid[1][0] == ‘#’ || grid[n-1][m-2] == ‘#’ || grid[n-2][m-1] == ‘#’) for unavailability of path.
Instead of this -
if(grid[0][1] == ‘#’ || grid[1][0] == ‘#’ || (grid[n-1][m-2] == ‘#’ && grid[n-2][m-1] == ‘#’))
Let’s consider a case -
n = 3 m = 3;
grid =
. . #
. # #
. . .
In this case, the answer should be 6, the robot 1 will follow the path of - (0,0)->(1,0)->(2,0)->(2,1)->(2,2) and robot 2 will follow - (0,0)->(1,0)->(0,0)->(1,0)->(2,0)->(2,1)->(2,2) so the answer should be 6 in this case. But the solution its accepting returns -1 for this case.
We should only have to check for the starting position because on the first time we move, the robots should be on different cells but for destination we have to check for at least an adjacent open cell of destination cell.