首页 > 代码库 > hdu1010
hdu1010
题目链接
题目:
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64987 Accepted Submission(s): 17738
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
Author
ZHANG, Zheng
这个题目是一道dfs。。。
也是奇偶性剪枝的典型例题。。。
奇偶性性剪枝的证明链接:
点击此处
有两个剪枝,首先是如果找到一种方法,就返回,不需要在找下去。。。否则是浪费时间。。。
而是奇偶性剪枝。。。
奇偶性剪枝具体代码:
(t-abs(enx-x)-abs(eny-y))%2
因为如果从一个点到重点的横纵坐标之和和奇数,若剩余时间为偶数,那么肯定就是不可能到达的。。反之剩余时间为奇数。。。一个点到重点的横纵坐标之和和偶数,那么同样是不可能到达的。。。就算转弯也不可能,因为转弯必定是偶数的增加,一来一回。。。。然后就是搜索了,从上,下,左,右四个方向搜索。。。。生成一颗解答树。。具体代码如下:
#include<cstdio> #include<cmath> #include<iostream> using namespace std; int n,m; const int maxn=7; int map[maxn][maxn],pos; int stx,sty,enx,eny; /* 奇偶性剪枝就是说如果给的一个点到另外一个点如果只有奇数步可以到,那么如果两点的横纵左边之和为偶数,那么肯定不会到达。 不管如何走,因为转弯的话,肯定也是偶数步。。所以奇偶性剪枝的原理就是这样 具体链接: */ int abs(int a) { if(a>=0) return a; else return -a; } int d[4][2]={0,1,1,0,0,-1,-1,0};//二维数组表示向上,向下,向左,向右的情况。。 void dfs(int x,int y,int t) { if(pos==1) return; else if(t==0) { if(x==enx&&y==eny) { pos=1; return; } else return; } else if(t<abs(enx-x)+abs(eny-y)||(t-abs(enx-x)-abs(eny-y))%2)/*奇偶性剪枝*/ return ; else { for(int i=0;i<4;i++) { int midx=x+d[i][0]; int midy=y+d[i][1]; if(midx>0&&midx<=n&&midy>0&&midy<=m&&(map[midx][midy]==‘.‘||map[midx][midy]==‘D‘)) { map[midx][midy]=‘X‘; dfs(midx,midy,t-1); map[midx][midy]=‘.‘; } } } return; } int main() { char str[10]; int i,j,t; while(scanf("%d%d%d",&n,&m,&t)!=EOF) { if(n==0&&m==0&&t==0) return 0; else { for(i=1;i<=n;i++) { scanf("%s",str); for(j=1;j<=m;j++) { map[i][j]=str[j-1]; if(map[i][j]==‘S‘) stx=i,sty=j; if(map[i][j]==‘D‘) enx=i,eny=j; } } pos=0; dfs(stx,sty,t); if(pos) printf("YES\n"); else printf("NO\n"); } } return 0; }
加油。。Gery!!!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。