In the problem CSES-Labyrinth I have written a simple BFS solution but still getting TLE , The solution is O(N*M) solution means it should work fine , but don't know why I'm getting TLE. Any Help is appreciated.
My Submission : Here
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3885 |
3 | jqdai0815 | 3682 |
4 | Benq | 3580 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3506 |
7 | ecnerwala | 3505 |
8 | Radewoosh | 3457 |
9 | Kevin114514 | 3377 |
10 | gamegame | 3374 |
# | User | Contrib. |
---|---|---|
1 | cry | 170 |
2 | -is-this-fft- | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 157 |
6 | Dominater069 | 156 |
7 | adamant | 153 |
8 | luogu_official | 152 |
8 | awoo | 152 |
10 | maomao90 | 148 |
In the problem CSES-Labyrinth I have written a simple BFS solution but still getting TLE , The solution is O(N*M) solution means it should work fine , but don't know why I'm getting TLE. Any Help is appreciated.
My Submission : Here
Name |
---|
From what I can see, you are storing the path for each iteration of the BFS in the queue. Since the maximum path length can be N*M and u are pushing each such path into the queue, the worst case tc is actually O((N*M)^2).
Try to retrace the path after reaching B.
Thanks for the help, I agree that the max path can be N*M indeed but by pushing such path into queue how the tc will become O((N*M)^2) ? can you explain ?
because pushing a string of length x to the queue itself will be an O(x) operation, the reason being the entire length of the string needs to be copied
I think you have overcomplicated here you are storing the path at each point leading to TLE here i think here you can use the bfs, here you just need to store a boolean vector for visited and you need to store the parent value also so that you can trace the path. The point here is we know that the bfs always gives the shortest path as soon as we reach the ending point we will simple backtrack through the parent in order to the get the path.
Thanks for the help, I got your point.