<!-- lang: cpp -->
#include<iostream>
using namespace std;
int map[9][9],dx,dy;
bool sou(int x,int y,int time)
{
if(x==dx&&y==dy&&time==0)
return true;
else if(x==dx&&y==dy)
return false;
if(!map[x+1][y])
{
map[x][y]=1;
if(sou(x+1,y,time-1))
return true;
}
if(!map[x-1][y])
{
map[x][y]=1;
if(sou(x-1,y,time-1))
return true;
}
if(!map[x][y+1])
{
map[x][y]=1;
if(sou(x,y+1,time-1))
return true;
}
if(!map[x][y-1])
{
map[x][y]=1;
if(sou(x,y-1,time-1))
return true;
}
map[x][y]=0;
return false;
}
int main()
{
int n,m,t,i,j,sx,sy;
char a;
while(cin>>n>>m>>t)
{
if((!n)&&(!m)&&(!t))
break;
for(i=0;i<9;i++)
for(j=0;j<9;j++)
map[i][j]=1;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
cin>>a;
switch(a)
{
case 'S':
map[i][j]=1;
sx=i;
sy=j;
break;
case 'D':
map[i][j]=0;
dx=i;
dy=j;
break;
case '.':
map[i][j]=0;
}
}
if(sou(sx,sy,t))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
转载于:https://my.oschina.net/fzyz999/blog/142729