首页 > 代码库 > HDU 1010 Tempter of the Bone (DFS 奇偶剪枝)
HDU 1010 Tempter of the Bone (DFS 奇偶剪枝)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 75141 Accepted Submission(s): 20531
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
Source
ZJCPC2004
一开始没看清楚用的是BFS果断错,后来用深搜TLE,但是搞懂了奇偶剪枝。
奇偶剪枝是数据结构的搜索中,剪枝的一种特殊小技巧。
现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,
s | ||||
| | ||||
| | ||||
| | ||||
+ | — | — | — | e |
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;
s | — | — | — | |
— | — | + | ||
| | + | |||
| | ||||
+ | — | — | — | e |
如图,为一般情况下非最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);
推广之,若 t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达;
返回,false;
反之亦反
简单来说就是偏移了最短路径要走偶数步才能走回来。
这道题目就是这样,当前的节点到终点,要走偶数步才能走回来。
如果是奇数步就走不到了。
#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<queue>#include<stdlib.h>#include<algorithm>#define LL __int64using namespace std;const int MAXN=100;int n,m,T,vis[MAXN][MAXN];int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};char map[MAXN][MAXN];bool flag;struct node{ int x,y;}star,en;bool judge(int x,int y){ if(x<0 || x>=n || y<0 || y>=m || vis[x][y] || map[x][y]==‘X‘) return false; return true;}void DFS(int x,int y,int cur){ if(flag) return ; if(cur==T) { if(x==en.x && y==en.y) flag=true; return ; } int dis=abs(x-en.x)+abs(y-en.y);//当前节点到终点 int res=T-dis-cur;//还剩下多少步 if(res%2==1) return ; for(int i=0;i<4;i++) { int xx=x+dir[i][0]; int yy=y+dir[i][1]; if(judge(xx,yy)) { vis[xx][yy]=1; DFS(xx,yy,cur+1); vis[xx][yy]=0; } }}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d %d %d",&n,&m,&T) && (n || m || T)) { int cnt=0; for(int i=0;i<n;i++) { scanf("%s",map[i]); for(int j=0;j<m;j++) { if(map[i][j]==‘S‘) { star.x=i; star.y=j; } if(map[i][j]==‘D‘) { en.x=i; en.y=j; } if(map[i][j]==‘X‘) cnt++; } } if(n*m-cnt-1<T){printf("NO\n");continue;} memset(vis,0,sizeof(vis)); flag=false; vis[star.x][star.y]=1; DFS(star.x,star.y,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
HDU 1010 Tempter of the Bone (DFS 奇偶剪枝)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。