HDU 1010 Tempter of the Bone DFS、枝切り

20445 ワード

Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 166686 Accepted Submission(s): 44241
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 4 4 5 S.X. …X. …XD … 3 4 5 S.X. …X. …D 0 0 0
Sample Output NO YES
問題の接続
問題の説明
1匹の子犬が迷宮で機関に触れ、迷宮の出口は閉まっており、t秒目(子犬が骨を拾ってから計算)にドアが開いた.そのレンガは陥没し、毎秒上下左右の隣接するレンガに向かって走ることができ、さっきのレンガは陥没し、迷路から脱出できるかどうかを聞くことができます.
もんだいぶんせき
ドアがt秒目にしか開いていないことと、前の秒の位置にあるレンガが陥没する特徴があるため、犬がt秒目に出口に到着しなければならない.BFSは最短の経路を見つけているので、この問題はBFSでは使えない.ではDFSを使います.しかし、枝を切らずに直接検索するとタイムアウトの問題が発生し、迷路から脱出できない状況が続くことが多いためだ.剪定は、不可能な状況をより早く終了させ、不要な検索時間を節約することができる.ここには2つの枝を切る場所があります:1、起点から終点までのマンハッタンの距離がドアを開ける時間の数値と奇偶的に異なると、t秒目に終点に着くことはできません.検索の過程で、常に所在点から終点までのマンハッタンの距離が残りのドアを開ける時間の数値パリティと異なるため、必ず残りの時間が0の場合、入り口までの距離は0ではありません.2、現在点から終点までのマンハッタンの距離の数値が残りのドアを開ける時間の数値より小さい場合、脱出にも成功しない.リファレンス
コードは次のとおりです.
#include
#include
#include
#include
using namespace std;
const int N=10;

bool flag;
int n,m,t;
int xd,xs,yd,ys;
int dirt[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

char chart[N][N];
bool vis[N][N]; 

int md(int x1,int y1,int x2,int y2){//      
	return abs(x1-x2)+abs(y1-y2); 
}

bool judge(int x,int y,int time){
	/*
	        :     、     、   、              、
	      'S'  
	*/ 
	if(flag||x<0||x>=n||y<0||y>=m||vis[x][y]||chart[x][y]=='X'||md(x,y,xs,ys)>t-time) return 0;
	if(time!=t&&chart[x][y]=='S') return 0;
	if(time==t&&chart[x][y]=='S') flag=1;
	return 1;
}

void dfs(int x,int y,int time){
	int xt,yt;
	for(int i=0;i<4;i++){
		xt=x+dirt[i][0];
		yt=y+dirt[i][1];
		if(judge(xt,yt,time+1)){
			vis[xt][yt]=1;
			dfs(xt,yt,time+1);
			vis[xt][yt]=0;
		}
	}
}

int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	while(cin>>n>>m>>t){
		if(!n&&!m&&!t) break;
		
		for(int i=0;i<n;i++) cin>>chart[i];
		for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
			if(chart[i][j]=='S'){
				xs=i;
				ys=j;
			}
			else if(chart[i][j]=='D'){
				xd=i;
				yd=j;
			}
		}
		int d=md(xd,yd,xs,ys);
		if(d>t||(d+t)%2) {
		//                       ,       
		//                  ,        
			cout<<"NO"<<endl;
			continue;
		}
		memset(vis,0,sizeof(vis));
		flag=0;
		vis[xd][yd]=1;
		dfs(xd,yd,0);
		if(flag) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}