#include<iostream>
#include<cmath>
using namespace std;
int di,dj;
bool escape;
int a,b,t;
char str[9][9];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
void bfs(int si,int sj,int cnt)
{
if(si<=0||sj<=0||si>a||sj>b)return ;
int i;
int temp=t-cnt-abs(si-di)-abs(sj-dj);
if(temp<0||temp%2)return ;
if(si==di&&sj==dj&&cnt==t)
escape=1;
if(escape) return ;
for(i=0;i<4;i++)
{
if(str[si+dir[i][0]][sj+dir[i][1]]!='X')
{
str[si+dir[i][0]][sj+dir[i][1]]='X';
bfs(si+dir[i][0],sj+dir[i][1],cnt+1);
str[si+dir[i][0]][sj+dir[i][1]]='.';
}
}
return ;
}
int main()
{
int i,j;
int si,sj;
while(cin>>a>>b>>t&&(a||b||t))
{
int wal=0;
for(i=1;i<=a;i++)
for(j=1;j<=b;j++)
{
cin>>str[i][j];
if(str[i][j]=='S')
{
si=i;sj=j;
}
if(str[i][j]=='D')
{
di=i;dj=j;
}
if(str[i][j]=='X')wal++;
}
escape=0;
if(a*b-wal<t)printf("NO\n");
else
{
str[si][sj]='X';
bfs(si,sj,0);
if(escape)printf("YES\n");
else printf("NO\n");
}
}
}
HDOJ1010 STILL DFS
最新推荐文章于 2018-04-06 23:22:01 发布