点击打开题目链接
Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 50372 Accepted Submission(s): 13552
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.
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.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
Sample Output
Author
ZHANG, Zheng
Source
ZJCPC2004
Recommend
JGShining
|
小狗在迷宫里发现了一个即将爆炸的炸弹,吓得它赶紧逃命。
小狗每秒钟只能选择移动到上下左右四个方向之一的空地上。
当小狗移动到一块空地上时,空地会很快下沉,因此小狗无法在一块空地上停留也无法再移动到这些走过的空地。
出口的门将在T秒后开启且开启时间小于一秒,由于小狗无法在一块空地上停留,所以小狗必须“恰好”在T秒后到达出口。
DFS+剪枝。
网上将其称之为奇偶剪枝,但个人认为从位移的角度解释更加简洁清晰,因此我称之为“位移剪枝”。思路如下:
对于从当前位置移动到出口的任意路径来说,除去位移所需的移动外,每一次多余的移动都必定有一次对应的反方向移动,因此
多余的移动次数必定是偶数。
比如说从当前位置到出口只需要往左移动三次,那么除去往左的三次移动之外,往左多移动一次就必须再往右移动一次,往右多
移动一次就必须再往左移动一次......
因此就有:小狗沿任意路径从当前位置到出口的移动次数(即当前剩余时间)= 位移所需移动次数 + 多余的移动次数
(必定是偶数)。
再由一个数字减去偶数之后奇偶性不变即可得到:当前剩余时间与位移所需移动次数奇偶性一致。
需要注意的是,剪枝只需对出发位置而不是DFS过程中的所有位置进行。证明如下:
若当前位置合法(当前剩余时间与位移所需移动次数奇偶性一致)。
当小狗从当前位置移动一次到达一个新的位置时,剩余时间减少一,奇偶性变化,位移所需次数变化一(减少一或者增加一),
奇偶性变化。
由于二者奇偶性同步变化,因此新的位置仍然合法。
由此可得:只要出发位置合法,那么由出发位置得到的其他所有位置也合法。
题意限定了小狗的移动次数为T,那至少得有T块空地给小狗走吧?所以必须有:
空地的数目 + 1(出口也算一块空地哦) ≥ T。
1、若当前移动次数为T但当前位置不是出口,剪枝。
2、若当前位置是出口但当前移动次数不为T,剪枝。
#include<math.h> #include<stdio.h> #define LEGCORD(X,Y) (0<=X&&X<N&&0<=Y&&Y<M) //合法坐标 #define MINSTEP(X1,Y1,X2,Y2) (abs(X1-X2)+abs(Y1-Y2)) //最短步数 const int Dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; int N,M,T,Sx,Sy,Dx,Dy,Flag,Empty; char Map[10][10]; void DFS(int X,int Y,int Cur) { int min=MINSTEP(X,Y,Dx,Dy); if(Cur+min>T) { return; } //步数剪枝 if(Cur==T) { Flag=(min==0); return; } //其他剪枝1 if(min==0) { Flag=(Cur==T); return; } //其他剪枝2 for(int n=0;n<4;++n) { int tmpx=X+Dir[n][0]; int tmpy=Y+Dir[n][1]; if(LEGCORD(tmpx,tmpy)&&(Map[tmpx][tmpy]=='.'||Map[tmpx][tmpy]=='D')) { Map[tmpx][tmpy]='X'; DFS(tmpx,tmpy,Cur+1); if(Flag) return; Map[tmpx][tmpy]='.'; } } } void ReaData() { Flag=Empty=0; for(int i=0;i<N;++i) { scanf("%*c%s",Map[i]); for(int j=0;j<M;++j) { if(Map[i][j]=='.') { ++Empty; } else if(Map[i][j]=='S') { Sx=i; Sy=j; } else if(Map[i][j]=='D') { Dx=i; Dy=j; ++Empty; } } } } int main() { while(scanf("%d%d%d",&N,&M,&T)==3&&(N||M||T)) { ReaData(); if((MINSTEP(Sx,Sy,Dx,Dy)&1)==(T&1)&&Empty>=T) //位移剪枝,地图剪枝 { DFS(Sx,Sy,0); } printf("%s\n",Flag?"YES":"NO"); } return 0; }