#include<stdio.h>
#include<string.h>
#include<math.h>
int vis[500][500];
char map[500][500];
int b[4][2]={ {0,1},{0,-1},{1,0},{-1,0} };
int flag;
int n,m,t;
int sx,sy,ex,ey;
void dfs(int x,int y,int count)
{
int dx,dy;
int i;
if(x==ex&&y==ey)
{
if(count==t)
flag=1;
return ;
}
if(count>=t)
return ;
if(map[x][y]!='X')
{
for(i=0;i<4;i++)
{
dx=x+b[i][0];
dy=y+b[i][1];
if(dx>=1&&dx<=n&&dy>=1&&dy<=m&&map[dx][dy]!='X'&&!vis[dx][dy])
{
vis[dx][dy]=1;
dfs(dx,dy,count+1);
vis[dx][dy]=0;
if(flag)
return ;
}
}
}
return ;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
int i,j;
int count;
if(n==0&&m==0&&t==0)
break;
for(i=1;i<=n;i++)
{
getchar();
for(j=1;j<=m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S')
{
sx=i;
sy=j;
}
if(map[i][j]=='D')
{
ex=i;
ey=j;
}
}
}
getchar();//这步别省了,会wrong的
if(abs(sx-ex)+abs(sy-ey)>t||(sx+sy+ex+ey+t)%2==1)//这步是剪枝,额初学者基本想不到吧
{
printf("NO\n");
continue;
}
/*for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf("%c",map[i][j]);
}
printf("\n");
}*/
memset(vis,0,sizeof(vis));
vis[sx][sy]=1;
count=0;
flag=0;
dfs(sx,sy,count);
if(flag==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
hdu 1010 Tempter of the Bone
最新推荐文章于 2022-12-04 15:41:50 发布