首页 > 代码库 > hdu 1010 Tempter of the Bone
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): 69074 Accepted Submission(s): 18972
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
<span style="font-size:18px;">#include<stdio.h> #include<math.h> #include<iostream> #include<algorithm> using namespace std; #define M 10 char map[M][M]; int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int n,m,k; int a,b,c,d; int flag; void dfs(int x,int y,int cur) { int hx,hy,i; if(cur==k && x==c && y==d) //跳出搜索的条件 { flag=1; return ; } if(flag) return ; if(cur>k) return ; if( (k-cur)%2 != (abs(x-c)+abs(y-d))%2 ) return ; // 奇偶剪枝 if(abs(x-c)+abs(y-d) > k-cur) return ; //路径剪枝 for(i=0;i<4;i++) { hx=x+dir[i][0]; hy=y+dir[i][1]; if(hx>=0 && hx<n && hy>=0 && hy<m) { if(map[hx][hy]!='X') { map[hx][hy]='X'; // 标记 dfs(hx,hy,cur+1); // 搜索 map[hx][hy]='.'; // 清空 } } } } int main () { int i,j,t; while(cin>>n>>m>>k) { t=0; if(n==0 && m==0 && k==0) break; for(i=0;i<n;i++) { for(j=0;j<m;j++) { cin>>map[i][j]; if(map[i][j]=='S') { a=i; //记录起点坐标 b=j; map[i][j]='X'; // 标记已经访问过 } else if(map[i][j]=='D') { c=i; d=j; // 记录终点坐标 } else if(map[i][j]=='.') t++; } } if(t+1<k) // '.'和'D'的个数比k小, 即可走的步数比要求的步数还少 { printf("NO\n"); continue; } flag=0; dfs(a,b,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; } </span>
奇偶剪枝:
把map看作
0 1 0 1 0 1
1 0 1 0 1 0
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
1 0 1 0 1 0
0 1 0 1 0 1
从 0->1 需要奇数步
从 0->0 需要偶数步
设所在位置 (x,y) 与 目标位置 (dx,dy)
如果abs(x-dx)+abs(y-dy)为偶数,则说明所在位置和目标位置的奇偶性相同,需要走偶数步
如果abs(x-dx)+abs(y-dy)为奇数,则说明所在位置和目标位置的奇偶性不同,需要走奇数步
那么abs(si-sj)+abs(di-dj) 的奇偶性就确定了所需要的步数的奇偶性!!!
(k-cur)表示剩下还需要走的步数,由于题目要求要在走k步恰好到达,那么 (k-cur) 与 abs(x-dx)+abs(y-dy) 的奇偶性必须相同.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。