hdu1010

Tempter of the Bone

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


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
 

Author
ZHANG, Zheng
 

Source

因为不是找最短路问题  而是步数恰好等于某一个数字  这里有bfs就不合适了  
正解 dfs + 奇偶剪枝
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10;
typedef long long ll;
int sx, sy, ex, ey;
int n, m, t;
int visit[maxn][maxn];
char mp[maxn][maxn];
bool getEnd;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x,int y,int step)
{
	if(getEnd) return ;
	
	if(x < 1 || y < 1 || x > n || y > m || mp[x][y] == 'X') return ;
	
	if(step > t) return ;
		
	if(mp[x][y] == 'D' )
	{
		if(step == t)
		getEnd = true;
		
		return ;
	}
	
	for(int i = 0;i < 4;i ++)
	{
		int dx = x + dir[i][0];
		int dy = y + dir[i][1];	
		if(!visit[dx][dy])
		{
			visit[dx][dy] = 1;
			dfs(dx,dy,step + 1);
			visit[dx][dy] = 0;
		}
	}
} 

int main()
{
	while(~scanf("%d%d%d",&n,&m,&t) && n && m && t)
	{
		getEnd = false;
		memset(visit,0,sizeof(visit));
		int numX = 0;
		for(int i = 1;i <= n;i ++)
		{
			for(int j = 1;j <= m;j ++)
			{
				cin >> mp[i][j];
				
				if(mp[i][j] == 'S')
				{
					sx = i;
					sy = j;
				}
				
				if(mp[i][j] == 'D')
				{
					ex = i;
					ey = j;
				}
				
				if(mp[i][j] == 'X')
				{
					numX ++;
				}
			}
		}
		//这个剪枝的意思是你需要走的步数对应图中必须有足够的空格才行。  不然 不可能走的通 
		if(n * m - numX < t)
		{
			cout << "NO" << endl;
			continue;
		}
		//奇偶剪枝,因为此题属于特殊要求  所走的步数必须恰好为 t  所以可以利用坐标特性
		//两个横坐标之差绝对值加上纵坐标之差绝对值的和就是需要走的步数 
		//起点横纵坐标之和的值为起点的奇偶性  同理  终点也是一样 所以当两个坐标奇偶性相同时需要走偶数布
		//不相同时则需要走奇数步  也就是 无论是哪种情况  
		// sx + ex + sy + ey + t  这五个数字相加一定为偶数  不然肯定到达不了 
		if(abs(sx - ex) + abs(sy - ey) > t || (sx + ex + sy + ey + t) % 2 == 1)
		{
			cout << "NO" << endl;
			continue;
		}
		visit[sx][sy] = 1; 
		dfs(sx,sy,0);
		
		if(getEnd)
		cout << "YES" << endl;
		else
		cout << "NO" << endl;
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/Soul_97/article/details/80056547
今日推荐