/*
zoj_2110 搜索
简单dfs,乍看以为dfs会超,写的时候才想到每一遍的搜索是很容易退出的。
无语的是写的时候居然将 for( i=0;i<4;i++ ) 写成 for( i=0;i<3;i++ )。。
又是浪费时间的debug才发现。。
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <string.h>
using namespace std;
char map[10][10];
bool flag[10][10];
int way[4][2]={ 0,1,0,-1,1,0,-1,0 };
struct point
{
int x,y;
};
point sta,end;
int n,m;
bool dfs( int s,int x,int y )
{
int i,xx,yy;
if( map[x][y]=='D' && s==0 ) return true;
if( map[x][y]=='D' && s!=0 ) return false;
if( s<=0 ) return false;
for( i=0;i<4;i++ )
{
xx=x+way[i][0] , yy=y+way[i][1];
if( xx>=0 && xx<n && yy>=0 && yy<m && map[xx][yy]!='X' && !flag[xx][yy] )
{
flag[xx][yy]=1;
if( dfs(s-1,xx,yy) )
return true;
flag[xx][yy]=0;
}
}
return false;
}
int main()
{
int t,i,j;
queue <point>q;
while( cin>>n>>m>>t && ( n||m||t ) )
{
memset( flag,0,sizeof(flag) );
for( i=0;i<n;i++ )
{
for( j=0;j<m;j++ )
{
cin>>map[i][j];
if( map[i][j]=='S' ) sta.x=i , sta.y=j ;
}
}
flag[sta.x][sta.y]=1;
if( dfs( t,sta.x,sta.y ) ) printf( "YES\n" );
else printf( "NO\n" );
}
return 0;
}
zoj 2110
最新推荐文章于 2018-01-14 21:29:32 发布