杭电oj 1010

先看题目

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 94609    Accepted Submission(s): 25643


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
 

说多了都是泪,我把这道题目的NO看成了FALSE,然后提交的时候都是wa,我就一直在找特殊的测试用例,但是找了一晚上没有找到,第二天随便看了一下,就发现错误了,哎,以后做事要仔细一点。

言归正传,这道题目的意思就是找迷宫内找到出口,找出口,大家都会想到用DFS,我也是用DFS深度搜索,需要注意的是,找到出口的时间也就是步数是一个规定值,不能多也不能少,而且走过的地方不可以返回。下面是我的代码:

#include 
using namespace std;
char maze[10][10];
int N,M,T;
bool visit[10][10];
bool flag;
bool canVisit(int x,int y)
{
	if(!visit[x][y])
		return true;
	return false;
}
void dfs(int number,int x,int y)
{
	if(number>T||flag)
	{
		return ;
	}
	
	else if(number==T)
	{
		if(maze[x][y]=='D')
			flag=true;
		return;
	}

	else if(maze[x][y]=='.'||maze[x][y]=='S')
	{
		visit[x][y]=true;
		//down 
		if(x0&&canVisit(x-1,y))
		{
			
			dfs(number+1,x-1,y);
		}
		if(y0&&canVisit(x,y-1))
		{
			dfs(number+1,x,y-1); 
		} 
		visit[x][y]=false;
	}
	

}
int main()
{
	int startX,startY;
	int i,j;
	while(cin>>N>>M>>T&&N&&M&&T)
	{
		for(i=0;i>maze[i][j];
				visit[i][j]=false;
				if(maze[i][j]=='S')
				{
					startX=i;
					startY=j;
				}
			}
		}
		flag=false;
		dfs(0,startX,startY);
		if(flag)
		{
			cout<<"YES"<

ac了

你可能感兴趣的:(杭电oj)