HDOJ 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): 84772    Accepted Submission(s): 23084


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
 
此文转载出处:点击打开链接
 
第一道迷宫搜索题,第一道用C++输入输出流的题目。详解见链接,代码如下:
 
#include<iostream>
#include<stdlib.h>
#include<cstring>
using namespace std;
char map[10][10];//存储迷宫地图 

bool dfs(int x,int y,int t)
{
	if(t==1)//剩一步时即可判断是否为出口,找到返回true   
	{
		if(map[x-1][y]=='D')
		   return true;//C++中的真假值表示方式 
		if(map[x+1][y]=='D')
		   return true;
		if(map[x][y-1]=='D')
		   return true;
		if(map[x][y+1]=='D')
		   return true;
		return false;
	}
	else
	{
		map[x][y]='X';//标记走过的
		//深度优先搜索 
		if(map[x-1][y]=='.'&&dfs(x-1,y,t-1))
		   return true;
		if(map[x+1][y]=='.'&&dfs(x+1,y,t-1))
		   return true;
		if(map[x][y-1]=='.'&&dfs(x,y-1,t-1))
		   return true;
		if(map[x][y+1]=='.'&&dfs(x,y+1,t-1))
		   return true;
		map[x][y]='.';//还原走过的,这里的操作方法还不太懂,多步还原如何回溯的没看懂 
		return false;
	}
}

int main()
{
	int n,m,t,i,j,sx,sy,endx,endy;
	while(cin>>n>>m>>t&&n!=0||m!=0||t!=0)
	{
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				cin>>map[i][j];
				if(map[i][j]=='S')
				{
					sx=i;sy=j;
				}
				else if(map[i][j]=='D')
				{
					endx=i;endy=j;
				}
			}
		}
		if((abs(sx-endx)+abs(sy-endy)-t)&1)//奇偶剪枝 
		    cout<<"NO"<<endl;
	    else 
		{
		    if(dfs(sx,sy,t))
		       cout<<"YES"<<endl;
	        else 
		       cout<<"NO"<<endl;
		} 
	}
	return 0;
}

你可能感兴趣的:(HDOJ 1010 Tempter of the Bone(dfs))