链接:hdoj1010
Tempter of the Bone
**Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 97697 Accepted Submission(s): 26504
**
Problem DescriptionThe 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.
InputThe 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.
OutputFor each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.Sample Input4 4 5S.X...X...XD....3 4 5S.X...X....D0 0 0Sample OutputNOYES
for (int i = 0; i < n; i += 1) { scanf("%s",maze_in); for (int j = 0; j < m; j += 1) { if (maze_in[j]=='X')maze[i][j]=0; else { maze[i][j]=1; if (maze_in[j]=='S'){ maze[i][j]=0; sx=i;sy=j; } if(maze_in[j]=='D'){ ex=i;ey=j; } } } }2、由于深搜的时间复杂度一般都很高,一定要考虑到剪枝该题涉及到的两个方面的剪枝:
a.奇偶剪枝,就是每相邻两步笛卡尔坐标和的奇偶性互异,这应该很显然。
b.曼哈顿距离剪枝,用深度下界判断当前状态是否可行。
int left=t-k,les=abs(x-ex)+abs(y-ey); if(les>left)return; if(left%2!=les%2)return;
其中t为目标时间,k为当前dfs时间;
3、千万要注意出发点的maze在预处理时一定要赋为不可到达;
if (maze_in[j]=='S'){ maze[i][j]=0; sx=i;sy=j; }
看下面这组数据你就明白了:
2 2 4
S.
.D
好了就这么多,下面是完整AC代码(c++):
#include<iostream> #include<stdio.h> #include <math.h> #include <cmath> using namespace std; int n,m,t,sx,sy,ex,ey; char maze_in[10]; int maze[10][10]; int dx[]={1,-1,0,0},dy[]={0,0,1,-1}; bool sol=false; void dfs(int x,int y,int k) { // cout<<x<<","<<y<<" "<<k<<endl; if(k>t)return; if(k==t&&x==ex&&y==ey) { sol=true; return; } if(sol)return; int left=t-k,les=abs(x-ex)+abs(y-ey); if(les>left)return; if(left%2!=les%2)return; for (int i = 0; i < 4; i += 1) { int nx=x+dx[i],ny=y+dy[i]; if (nx>=0&&ny>=0&&nx<=n&&ny<=m&&maze[nx][ny]) { maze[nx][ny]=0; dfs(nx,ny,k+1); maze[nx][ny]=1; } } } int main() { while (scanf("%d %d %d",&n,&m,&t)&&n) { sol=false; for (int i = 0; i < n; i += 1) { scanf("%s",maze_in); for (int j = 0; j < m; j += 1) { if (maze_in[j]=='X')maze[i][j]=0; else { maze[i][j]=1; if (maze_in[j]=='S'){ maze[i][j]=0; sx=i;sy=j; } if(maze_in[j]=='D'){ ex=i;ey=j; } } } } dfs(sx,sy,0); if(sol)printf("YES\n"); else printf("NO\n"); } return 0; }