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 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
'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
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;
}