Any ideas of how to solve this problem ??
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
2 | maomao90 | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
Any ideas of how to solve this problem ??
Название |
---|
The total number of states is 9!, and there are at most 4 moves to be made at each state, so it's possible to do a complete search with a BFS.
The constant factor for this approach is rather high, but it should work under the time limit if done carefully.
I tried to use BFS but I don't understand when will I reach "The Most Distant State"
Should i push the string containing the path along with a vector of the 9 states, is it good enough for the time limit ??
If you do the following:
Question — wouldn't the last item on the deque be the one you need? so you can just take amount from move counter of last item on the deque and this should be the result.
If you do a BFS, the states will be visited by increasing distance from the initial state, so the one you're looking for will always be the last one you visit.
You can check if a state has been visited by using a map in C++. And you can store both the state and the history of moves using strings.
I coded it that way and it got accepted.
This is all i could come up with, but it does not even give correct sample:
My code
Inside your BFS, when you try the 4 possibles moves, after you swap the zero with an adjacent element and push the state to the queue, you forgot to swap again to return the array to its original state. Add that swap and you'll get accepted.
Oh, and next time, please paste your code in pages like pastebin or ideone and post the link in your comment instead of the whole source code. It will save you lots of downvotes ;)
Comment is edited, and i Got AC with good Runtime
Thanks :D