HDU 1010 Tempter of the Bone(深搜+剪枝)

Tempter of the Bone

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


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
 
  
NOYES
 

Author
ZHANG, Zheng
 

Source

奇偶剪枝:(百度)
把矩阵标记成如下形式:
0,1,0,1,0
1,0,1,0,1
0,1,0,1,0
1,0,1,0,1
很明显,如果起点在0 而终点在1 那显然 要经过奇数步才能从起点走到终点,依次类推,奇偶相同的偶数步,奇偶不同的奇数步
在读入数据的时候就可以判断,并且做剪枝,当然做的时候并不要求把整个矩阵0,1刷一遍,读入的时候起点记为(si,sj) 终点记为(ei,ej) 判断(si+sj) 和 (Di+Dj) 的奇偶性就可以了

------------------------

看这个doggie是否能在第T秒刚好到达门。(来自https://blog.csdn.net/narzisen/article/details/80545804)      

因为两点之间直线最短,所以由S到达D的最短路径就是abs(sx-dx)+abs(sy-dy),可以把t理解为题目要你走的总步数,t-step就代表你任意走的总步数,因为abs(sx-dx)+abs(sy-dy)已经是最短的步数了,你走的一定不可能比它更短,所以t-step肯定是要>=最短步数的。所以当t-step<abs(sx-dx)+abs(sy-dy)的时候,我们就给它剪出去。(注意sx,sy,step都是在变化的

还有t-step-abs(sx-dx)-abs(sy-dy)必须是偶数,如果是奇数就不能到达D,给个例子大家看看吧 

3行4列    最短距离是5。红色箭头代表随意走的路线,一共9步,9-5=4为偶数,刚好到达D,如果为奇数,那最后一步到达的肯定是D的周围而不能刚好到达D。

S  
→ D

------别人的思路-------
从点(x0,y0)到点(x1,y1)的最短移动距离 dis= abs(x0-x1)+ abs(y0-y1);也就是说在水平方向和竖直方向要移动的最短距离和,在此题中 要在余下的有限时间 t内移动的目标位置,就要判断: t-dis 是奇数还是偶数,【因为假设t-dis=1,那么时间就要多1,而在一个时间单位内无论向哪个方向移动,都会与目标位置相对距离错一个,相反如果为偶数的话,就可以认为:在多余的(t-dis)/2 时间内往某方向移动,接下来在剩余的(t-dis)/2 时间内在按照原路返回】

#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
int flag,sx,sy,ex,ey,num;
int n,m,t,vis[10][10];
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
string map[10];
void dfs(int x,int y,int sum)
{
	if(flag==1)
	return ;
	if(x==ex&&y==ey&&sum==t)
	{
		flag=1;
		return ;
	}
	int mindis=abs(x-ex)+abs(y-ey);//当前点到终点的最短距离
	int p;
	p=(t-sum)-mindis;//t-sum表示到达终点还需要走多少步.t-sum和mindis的奇偶性必然相同 相同就是偶数不同就是奇数 
	if(p<0||p%2!=0)
	return ;
	for(int i=0;i<4;i++)
	{
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&map[xx][yy]!='X')
		{
			vis[xx][yy]=1;
			dfs(xx,yy,sum+1);
			vis[xx][yy]=0;
		}
	 } 
}
int main()
{
	while(cin>>n>>m>>t)
	{
		if(n==0&&m==0&&t==0)
		break;
		num=0;//记得初始化 
		for(int i=0;i<n;i++)
		{
			cin>>map[i];
			for(int j=0;j<m;j++)
			{
				if(map[i][j]=='S')
				{
					sx=i;
					sy=j;//记录起点 
				}
				if(map[i][j]=='D')
				{
					ex=i;
					ey=j;//记录终点坐标 
				}
				if(map[i][j]=='X')
				{
					num++;//记录墙的数量 
				 } 
			}
		}
			
		if(n*m-num-1<t)
		{
			cout<<"NO"<<endl;
			continue;
		}
		flag=0;
		memset(vis,0,sizeof(vis));
		vis[sx][sy]=1;
		dfs(sx,sy,0);
		if(flag) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
	}return 0;
 } 


猜你喜欢

转载自blog.csdn.net/qq_40046426/article/details/81041655