hdu-1010 Tempter of the Bone(DFS之奇偶剪枝)

Tempter of the Bone

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 95   Accepted Submission(s) : 35
Font: Times New Roman | Verdana | Georgia
Font Size:  

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
   
   
题意:这个题目的意思是给定你起点S,和终点D,问你是否能在 T 时刻恰好到达终点D。
分析:这样一看很明显是DFS,不过里面涉及到很多剪枝。
 
奇偶剪枝:
是数据结构的搜索中,剪枝的一种特殊小技巧。
现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,
 
s    
|    
|    
|    
+e
 
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;
  
s 
 + 
|+   
|    
+e
 
如图,为一般情况下非最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);
故,若t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;
返回,false;
反之亦反。

#include <iostream>

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,ss;
char map[10][10];
int cai[10][10];
struct point
{
int x,y,step;
};
int dir[4][2]={
{-1,0},{0,1},{1,0},{0,-1}
};
point start,end1;
int ans;
void DFS(point s)
{
if(ans==1) return ;
cai[s.x][s.y]=1;
int x,y,i;
point t;
for(i=0;i<4;i++)
{
x=s.x+dir[i][0];
y=s.y+dir[i][1];
//cout<<x<<" "<<y<<endl;
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='X'&&cai[x][y]==0)
{
t.x=x;t.y=y;t.step=s.step+1;
if(t.x==end1.x&&t.y==end1.y&&t.step==end1.step)
{
ans=1;return ;
}
else if(t.x==end1.x&&t.y==end1.y&&t.step!=end1.step)
{
continue;
}
else if(t.step==end1.step) break;
DFS(t);

}
if(ans==1) return ;
}
cai[s.x][s.y]=0;
}
int ans1;
void dfs1(point s)
{
if(ans1==1) return ;
cai[s.x][s.y]=1;
int x,y,i;
point t;
for(i=0;i<4;i++)
{
x=s.x+dir[i][0];
y=s.y+dir[i][1];
//cout<<x<<" "<<y<<endl;
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='X'&&cai[x][y]==0)
{
t.x=x;t.y=y;t.step=s.step+1;
if(t.x==end1.x&&t.y==end1.y)
{
ans1=1;return ;
}
dfs1(t);
}
}
}
int main(int argc, char *argv[])
{
int i,j;
int sum;
while(scanf("%d%d%d",&n,&m,&ss)!=EOF)
{

if(n==0&&m==0&&ss==0) break;
memset(cai,0,sizeof(cai));
ans=ans1=0;
sum=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("\n%c",&map[i][j]);
if(map[i][j]=='S') {start.x=i;start.y=j;start.step=0;}
else if(map[i][j]=='D') {end1.x=i;end1.y=j;end1.step=ss;}
if(map[i][j]!='X') sum++;
}
}
int num=abs(start.x-end1.x)+abs(start.y-end1.y);
if(ss>=sum||(ss-num)%2==1) ans=0;
else
{
dfs1(start);
memset(cai,0,sizeof(cai));
if(ans1==1) DFS(start);
else ans=0;
}
if(ans==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;

}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值