Tuesday, April 22, 2014

SPOJ 7188. Escape the Jail (ESJAIL)


   Here is what we will basically do. We need to use BFS and check, every time whenever we are checking a node we need to look over it's neighbors and see if there is one neighbor which we were not able to visit because it is locked we need to push that same node again to the end of the queue so that after our search it is possible that we can unlock the door which we weren't able to visit from the beginning. And we will need some optimizations, we can keep a double element queue where the first element shows the node and the second one shows the "time", whenever the time becomes too much we can exit the process and say that we can'r reach our destination. (in our case too much time can mean that time is more than 2*M). Also we can keep a boolean array which shows the elements we have double pushed to the array because there was a closed neighbor so that we won't have to push the same node many times. Check the source code for a better understanding.


No comments:

Post a Comment