Codeforces 648 Div2 D

Can someone please tell me why am i receiving tle on test 9 in Problem D (Solve the maze)
Link to the problem :
Link to my solution:

Is it because of dfs?

if you check via dfs every time you will get a TLE !!
check mine->

1 Like

You can only do DFS/BFS once by starting at the last cell in the last row and last column. Then check if you can visit all Good places or not, instead of checking if you can reach last cell for every Good place.

1 Like

Got it.
Thank You.