Hey! I hope you all are doing good.
I have been trying to figure out the solution for the problem. I am maintaining the state as [x][y][prevDir] as, the prevDir i took to reach [x][y], and pushing the new co-ordinate with same steps if newDir is same as prevDir else steps + 1, but its kinda confusing how to fix the direction for any cell as for example:
If the grid is:
S . .
X X .
F . .
We can reach grid[2][0] by [0][2] and [0][1] and now its kinda confusing what direction I should give [1][2] to move forward, as I know it should have [1][2][D] as it will give me optimal solution but [0][1] pushes [1][2] first so it sets [1][2][RD]. I hope I was able to explain my confusion.
Code
Thank you!!
Just from your question I'm guessing you need a 0-1 BFS. Use deque instead of queue, and if the direction doesn't change, and you don't increase the "steps", then use dequeue .push_front(), and if the steps increase + 1, then push the new state with .push_back(). This way all the best states (with the least steps) will resolve before all the worse states. Good luck
And you probably need a seperate dist[x][y][dir] for every direction, not just for every position.
I am sorry but i didn't understand the later part of pushing back and front as if we are in (0,1) we will push (0,2) front and (1,2) to the back, now as (0,2) will be processed if will also change direction. So the (1,2) from (0,2) will also be pushed to the back and it will be processed after the one pushed from (0,2) and it will hold the direction RightDiagonal but I want it to hold Down, hence my answer can be optimal.