@rsaha77

for input

2 2

01

01

your code answer is

1 0

2 0

but the correct answer is

1 0

1 0

This is because ur pushing the nodes into queue first and then while u r poping the elements from queue then ur updating the grid array value .

in above example at begining the elements in queue are {(1,2),(2,2)}

now ur poping (1,2) and the nodes connected to it are (1,1) and (2,2).u will only push (1,2) (with z value 0+1) into queue and not (2,2) because (2,2) is having grid value 0.after that u are poping (2,2) from grid and the nodes connected to it are (1,2)and(2,1) u will push (1,2) only into queue (with z value 0+1) into queue.

Now ur queue is {(1,2)(with z value 1) and(2,1)(with z value 1)}

now u will pop (1,2) and the node connected to it with grid value -1 are (2,1)

so now ur pushing (2,1)(with z value 1+1).

now ur queue is having values {(2,1)(with z value 1) and(2,1)(with z value 2)}

finally u are ended with grid[2][1]=2

so this the error…