hdu-1010(dfs的剪枝)

Tempter of the Bone

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


 

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

ZJCPC2004

 

Recommend

JGShining   |   We have carefully selected several similar problems for you:  1016 1241 1242 1072 1312 

 

Statistic | Submit | Discuss | Note

心得:1、用dfs,以找到结尾点和t符合条件结束,每次要先标记,再还原。

2、奇偶剪枝:最短路径的长度l=(abs(ex-x)+abs(ey-y))是曼哈顿距离

(曼哈顿距离:

例如在平面上,坐标(x1, y1)的i点与坐标(x2, y2)的j点的曼哈顿距离为:

d(i,j)=|X1-X2|+|Y1-Y2|.)

所以每次判断要求距离(时间)和最短距离的差值,如果是<0或者是奇数就是不符合条件,直接结束就行了,

这样可以防止时间超限。

#include<bits/stdc++.h>
using namespace std;
char a[10][10];
int vis[10][10];
int fx[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n,m,t,fg;
int sx,sy,ex,ey;

int check(int x,int y)
{
	if(vis[x][y]) return 1;
	if(a[x][y]=='X') return 1;
	if(x<0||y<0||x>=n||y>=m) return 1;
	return 0;
}
void dfs(int x,int y,int ans)
{
	if(x==ex&&y==ey&&ans==t) 
	{
		fg=1;
		return ;
	}
	if(fg) return ;
	int tp=(t-ans)-(abs(ex-x)+abs(ey-y)); //路径与最短路径的差值。 
	if(tp<0||tp&1) return ; //判断 tp<0或者tp是偶数就返回(tp&1,是判断tp的最后一位是1返回1,是0,返回0) 
	for(int i=0;i<4;i++)
	{
		int x1=x+fx[i][0];
		int y1=y+fx[i][1];
		if(check(x1,y1)) continue;
		vis[x1][y1]=1;
		dfs(x1,y1,ans+1);
		vis[x1][y1]=0;
	}
}
int main(void)
{
	int i,j;
	while(~scanf("%d%d%d",&n,&m,&t))
	{
		if(n==0&&m==0&&t==0) break;
		memset(a,0,sizeof(a));
		memset(vis,0,sizeof(vis));
		int cnt=0;
		for(i=0;i<n;i++)
		scanf("%s",a[i]);
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(a[i][j]=='X') cnt++;
				else if(a[i][j]=='D') ex=i,ey=j;
				else if(a[i][j]=='S') sx=i,sy=j;
			}
		}
		fg=0;
		vis[sx][sy]=1;
		dfs(sx,sy,0);
		if(fg) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
} 

参考文章:https://blog.csdn.net/hurmishine/article/details/51255092

猜你喜欢

转载自blog.csdn.net/qq_41829060/article/details/81360596