№ | Пользователь | Рейтинг |
---|---|---|
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 | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
8 | Dominater069 | 154 |
8 | nor | 154 |
Название |
---|
I don't know if I'm wrong but my idea is that you should follow always an optimal path from the etrance to the treasure. Optimal path — a path with minimum number of traversed spikes. Then you should follow this same path on the way back to the entrance. If the number of spikes in the optimal path multiplied by 2 is higher than the number of allowed spike traversals then the answer is clearly that it's impossible to reach the treasure, otherwise it's possible.
A really straight-forward approach is to think of the maze as a graph and set the cost of edges that get you into a spike or out of a spike to 0.5. This way you can use a minimum cost path algorithm to find the optimal solution.
Another idea is to use a kind of smart flood-fill technique(BFS). First you should flood from the etrance all the cells without spikes, and mark them as visited. If you didn't reach the destination after this, push all the addiacent spike cells that are available from the flooded cells and add +1 to the global cost. Then do a flood-fill from the spiked cells, without visiting the cells you flooded on the previous path because it makes no sense to go to a spike cell then come back to a normal cell. Repeat this recursively until you reach the destination cell, or the global cost exceeds number_of_allowed_spikes/2.
but if i had a case like this : 4 4 1 @@@@ ..s. ss.. x...
4 4 1 @@@@ ..s. ss.. x...
It looks like I misread the statement and thought there was only 1 entrance. I submited my solution with the second idea mentioned above and it was accepted, but it looks like it worked luckily for me, because in my code I was adding all the entraces to the initial queue, instead of just 1 like I said and wanted to.
Now what's left for the algorithm I mentioned above is to prove that we must always follow the optimal way, even if we have multiple entrances. Let's call the optimal path A->X, where A is one of the initial entrances. Why do we follow this path? Because it leads us to a way with a minimal number of traversed spikes from an entrance to the treasure. Now let's say we reach the treasure X. Should we follow our way back to A or choose a path to another entrance? Let's say that there is better path from X to an entrance C, so let's call this path X->C. It follows that F(A->X)>F(X->C) where F is the function that gives us the number of minimal spikes traversed on a certain path. It's also obvious that F(@->X)=F(X->@) where @ is a random entrance.
But if F(A->X)>F(X->C), then we shoud have chosen the path C->X from the beginning, so it contradicts that A->X is an optimal path. (it actually comes to the fact that there is a more optimal path than the optimal path, so it leads to an absurd statement). This way we proved that we always need to follow the optimal path from the start to the finish, and from the finish to the start.
Here is my code that worked: http://ideone.com/x7MYPB If you want to do it yourself, then you should better not open it.
thank you very much :-)
i'll try to implement it myself first :)