hdu 1010 Tempter of the Bone(bfs)
hdu 1010 Tempter of the Bone
http://acm.hdu.edu.cn/showproblem.php?pid=1010
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.
'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.
//剪枝深度搜索
#include<iostream>
#include<cmath>
using namespacestd;
intm,n,t;
charmap[10][10];
boolmark[10][10];
intex,ey,flag;
intcx[]={0,0,1,-1};
intcy[]={-1,1,0,0};
voiddfs(intsx,intsy,intstep)
{
inti;
if(flag) return;
if(sx==ex&&sy==ey&&step==t)
{
flag=1;
return;
}
inttemp=(t-step)-abs(sx-ex)-abs(sy-ey);
if(temp<0||temp%2!=0)
return;
for(i=0;i<4;i++)
{
intdx=cx[i]+sx;
intdy=cy[i]+sy;
if(dx>=0&&dx<m&&dy>=0&&dy<n&&!mark[dx][dy]&&map[dx][dy]!='X')
{
mark[dx][dy]=1;
dfs(dx,dy,step+1);
if(flag)return;
mark[dx][dy]=0;
}
}
}
intmain()
{
while(~scanf("%d%d%d",&m,&n,&t))
{
if(n==0&&m==0&&t==0)
break;
inti,j,sx,sy;
for(i=0;i<m;i++)
{
getchar();
for(j=0;j<n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S')
{
sx=i;
sy=j;
}
else if(map[i][j]=='D')
{
ex=i;
ey=j;
}
}
}
getchar();
flag=0;
memset(mark,0,sizeof(mark));
mark[sx][sy]=1;
dfs(sx,sy,0);
if(flag)
puts("YES");
else
puts("NO");
}
return0;
}