Link to the question:- https://www.interviewbit.com/problems/rook-movement/
I tried BFS where in each state I store min steps,direction (up,down,left or right). I am getting TLE for large inputs.
Can someone help me ?
Thanks.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Link to the question:- https://www.interviewbit.com/problems/rook-movement/
I tried BFS where in each state I store min steps,direction (up,down,left or right). I am getting TLE for large inputs.
Can someone help me ?
Thanks.
Название |
---|
Why do you need to store the direction? BFS will never revisit the same cell due to the shortest number of steps property. And I also don't understand how you can get TLE because the input is small. Try posting your code here.
I thought that direction is required.
Consider the eg:-
000111
000000
where '1' denotes obstacles.
We can visit (1,2) in 2 steps in 2 ways from (0,0):-
1) (0,2) and (1,2)
2) (1,0) and (1,2).
Now if destination is (1,3), we would prefer 2nd) option over first as using first option would give 3 steps as answer but optimal answer to reach (1,3) should be 2.
Anyway, your method works as well. Just that I don't understand how you got TLE. Posting some code would certainly help people to determine the cause of the problem.
Added my code.