HDU 1813 Escape from Tetris
Escape from TetrisTime Limit: 12000/4000 ms (Java/Other) Memory Limit: 32768/32768 K (Java/Other) Total Submission (s): 1 Accepted Submission (s ): 1 Problem Description Lele was finally beaten by fire because it was facing the Board all night. When he goes to bed every day, he will dream that he will be thrown into a board and cannot find a way out. Then he will wake up from his dream. Over time, Lele has become mentally weak. Whether a dream will become a reality, no one can say, but not afraid of 10 thousand just in case. Now every time Lele sees a chessboard, he will imagine how to escape after being locked in.
Lele's chessboard is square. Some of the grids are bad and cannot go. The rest can go. As long as you walk to the edge of the Board (the outermost circle), you have escaped. Lele dreamed that he would be thrown into a grid that could be taken, but he was not sure which one he was, so he had to be prepared to be thrown into any grid.
Now, Lele can help you to find a shortest sequence for any board. The sequence can include north (up in the MAP) and east (to the right in the map ), south (down in the map), west (left in the map), the four direction commands. No matter which good grid Lele is thrown into, it can walk out of the board in this sequence.
The specific method to escape is: no matter where Lele is thrown, Lele executes commands one by one in the direction of the sequence, and each command follows a grid. If it encounters a bad grid, ignore this command. Of course, if you have escaped, you can ignore the remaining commands in the sequence.
Input this question contains multiple groups of tests, please process until the end of the file. The first line of each group of tests contains a positive integer N (0