hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)
Tempter of the Bone |
||||||||||||||||||||||||||||||||||||||||||||||||||
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) | ||||||||||||||||||||||||||||||||||||||||||||||||||
Total Submission(s): 615 Accepted Submission(s): 219 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Problem Description
The doggie found a bone in an ancient maze, which fascinated him a
lot. However, when he picked it up, the maze began to shake, and the
doggie could feel the ground sinking. He realized that the bone was a
trap, and he tried desperately to get out of this maze.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him. |
||||||||||||||||||||||||||||||||||||||||||||||||||
Input
The input consists of multiple test cases. The first line of each
test case contains three integers N, M, and T (1 < N, M < 7; 0
< T < 50), which denote the sizes of the maze and the time at
which the door will open, respectively. The next N lines give the maze
layout, with each line containing M characters. A character is one of
the following:
\\\\\\\'X\\\\\\\': a block of wall, which the doggie cannot enter; \\\\\\\'S\\\\\\\': the start point of the doggie; \\\\\\\'D\\\\\\\': the Door; or \\\\\\\'.\\\\\\\': an empty block. The input is terminated with three 0\\\\\\\'s. This test case is not to be processed. |
||||||||||||||||||||||||||||||||||||||||||||||||||
Output
For each test case, print in one line \\\\\\\"YES\\\\\\\" if the doggie can survive, or \\\\\\\"NO\\\\\\\" otherwise. |
||||||||||||||||||||||||||||||||||||||||||||||||||
Sample Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0 |
||||||||||||||||||||||||||||||||||||||||||||||||||
Sample Output
NO YES
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;
如图,为一般情况下非最短路径的任意走法举例,step2=14; |
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; //迷宫 char maze[10][10]; //标记此处是否走过,0为未走过,1为走过 int mark [10][10]; //四个方向 上 下 左 右 int direct[4][2] = {-1,0,1,0,0,-1,0,1}; //迷宫行列,和开门时间 int row,column,t; //开始位置,和门的位置 int begin_x,begin_y,end_x,end_y; //r,c 表示搜索的位置,step表示走的步子也等于时间 bool dfs(int r,int c,int step) { int x,y; //成功了,成功逃脱 if(r==end_x&&c==end_y&&step==t) return true; //剪枝,时间已经到了,逃脱失败 if(step>=t)return false; //奇偶剪枝...........没有这个剪枝,超时。。。呜呜。。 int temp=t-step-abs(r-end_x)-abs(c-end_y); if (temp<0 || temp%2!=0)return false; for(int i = 0; i < 4; i++) { x = r + direct[i][0]; y = c + direct[i][1]; //出界了 if(x<0||x>=row||y<0||y>=column) continue; //此路不通 if(maze[x][y]=='X')continue; //该位置已经走过了。。 if(mark[x][y]==1)continue; mark[x][y] = 1; if(dfs(x,y,step+1))return true; mark[x][y] = 0; } return false; } int main() { int wall; while(cin>>row>>column>>t&&row!=0&&column!=0&&t!=0) { wall = 0; //初始化,标记为未走过 for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) mark[i][j] = 0; for(int i = 0; i < row; i++) { scanf("%s",maze+i); for(int j = 0; j < column;j++) if(maze[i][j]=='S') { begin_x = i; begin_y = j; }else if(maze[i][j]=='D'){ end_x = i; end_y = j; }else if(maze[i][j]=='X')wall++; } //剪枝一 if(row*column-wall<=t) { printf("NO\n"); continue; } mark[begin_x][begin_y]=1; if(dfs(begin_x,begin_y,0)) printf("YES\n"); else printf("NO\n"); } return 0; }
posted on 2012-10-09 16:28 NewPanderKing 阅读(603) 评论(0) 编辑 收藏 举报