hdu 1010
empter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 88587 Accepted Submission(s): 24116
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.
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.
'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
题意:小狗要到一个地方去,有一定的时间,要在规定时间到达那个地方
分析:dfs+剪枝;剪枝一:当剩下的步数大于剩下的时间的时候,狗是不能走到的
剪枝二:小狗在的地方,奇偶性和终点的奇偶性相同,狗要走偶数步,不同要走奇数步,这个应该好理解吧,坐标x,y相加对二取余就是他的奇偶性,将现在坐标和终点坐标四个点加起来看和剩余时间奇偶性是否相同
#include<cstdio> #include<cstring> #include<iostream> using namespace std; char map[10][10]; int p[4][2]={{-1,0},{1,0},{0,1},{0,-1}};//方向,刚学,代码写的挫 int i,j,flag,n,m,t,stx,sty,enx,eny; int abs(int a){ return a>0?a:-a; } void dfs(int x,int y,int k) { if(t==k&&enx==x&&eny==y) flag=1; if(flag) return;//不知道当初为什么这么写,直接return不就好了吗,日哦,可能刚开始学掌握的不好吧,好多代码都是参考别人的才写出来的 int v=t-k-abs(enx-x)-abs(eny-y);//两个剪枝,v小于0时就是剪枝1,判断v的奇偶,剪枝2 if(v<0||v&1) return; for(int i=0;i<4;i++) { int xx=p[i][0]+x; int yy=p[i][1]+y;//枚举方向 if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]!='X') { map[xx][yy]='X'; dfs(xx,yy,k+1); map[xx][yy]='.';//回溯 } } } int main(){ while(scanf("%d%d%d",&n,&m,&t)){ if(n+m+t==0) break; for(i=0;i<n;i++){ cin>>map[i]; for(j=0;j<m;j++){ if(map[i][j]=='S'){ stx=i; sty=j; }//记录起点和终点 if(map[i][j]=='D'){ enx=i; eny=j; } } } map[stx][sty]='X'; flag=0; dfs(stx,sty,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }