BFS - SHORTEST PATH Challenge

In my second test case, I’m getting time limit. I am unable to think of a better solution than my current solution and I need help (https://ide.codingblocks.com/s/149259)

If you are visiting a node for the first time, then you have to mark it as visited.


Check the above code

I’ve already marked the current(visiting) node as visited on line no. 51

Check the above code for reference, it won’t give TLE

I could have looked on GeeksForGeeks for the solution but I want the logical mistake in my code. Could you please tell me in my code, where am I doing mistake ?

  1. Your code will give the correct output, so the approach is correct, but its not the most optimal approach.
  2. From what you wrote it appeared that you are looking for a better approach
    That’s why that code was shared. If you feel that any other website offers a clearer picture, then you may choose to read it from there.

I don’t want to browse other websites, I said that I could have looked but I’m unable to find the optimal solution even after checking out the code link that you commented above that’s why I’m asking you that where am I doing mistake in my code or you can also tell me what approach should I take manually and I’ll implement that in code. You have seen my current approach.