Maze
Time Limit: 4000MS | Memory Limit: 65536K | |||
Total Submissions: 792 | Accepted: 140 | Special Judge |
Description
You have been blindfolded and deposited someplace in a maze. You have no idea where you are. You do know, however, that the maze is laid out on a grid, and that each grid location is either blocked or free. In fact, you have memorized a map of the maze. Also, your magnetic personality allows you to always sense which direction is north.
In this maze, you have four possible moves: north, south, east, and west. Your task is to find the shortest sequence of moves that will guarantee your escape, regardless of your initial placement in the maze. You have "escaped" whenever you reach a square on an outside edge of the grid (and if you start there, then you've already escaped). Further moves are irrelevant once you have escaped. If you try to walk into a wall, you will simply stay in the same spot.
You may assume that it is possible to escape from every unblocked position in the maze.
In this maze, you have four possible moves: north, south, east, and west. Your task is to find the shortest sequence of moves that will guarantee your escape, regardless of your initial placement in the maze. You have "escaped" whenever you reach a square on an outside edge of the grid (and if you start there, then you've already escaped). Further moves are irrelevant once you have escaped. If you try to walk into a wall, you will simply stay in the same spot.
You may assume that it is possible to escape from every unblocked position in the maze.
Input
Input consists of a positive integer n <= 8, followed by n lines giving the rows of an n by n grid. This grid describes the maze you are trapped in. Written on the screen, north is up. Blocked locations are denoted by the character "O" (that's an uppercase "o"), while unblocked locations are indicated by the character ".".
Output
Output consists of a number of lines, each consisting of one of "north", "south", "east", or "west", indicating the shortest sequence of moves that guarantees escape for any possible unblocked starting position.
If there are multiple possible shortest sequences, any of them will do.
If there are multiple possible shortest sequences, any of them will do.
Sample Input
4 OO.O ...O OO.. O..O
Sample Output
east north
Source
分析:这题貌似只能用DFS,BFS爆内存,哎~~~改了我好久,还有DFS我居然变得这么挫,连标记都写错了
转别人的。。。。
简要题解:
DFS+剪枝+Hash判重。
对于一个n*n局面,只要保存中间(n-2)*(n-2)的boolean值就可以了,也就是最多36个。转成二进制,在mod一个大数用来判重(如果2个不同的局面hash值一样怎么办。。。不鸟它。。。)
强剪枝:已使用的步数+当前局面中所有点离出口的距离>=ans,就剪枝!
一个容易WA的地方:ans初始为(n-2)*(n-2)+1,实际上ans的上界为(n-2)*(n-2),那为神马要初始成(n-2)*(n-2)+1?因为有可能答案只有一步,而比较的时候是if step<ans then ans:=step;
所以就没有将走法数组更新!
简而言之就是这样的数据
3
…
…
…
话说我是将图转换为n-2个二进制数,与题解不同
代码(好挫啊):