标签:
简单的dfs应用,注意have数组的处理。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<list> #include<deque> #include<vector> #include<algorithm> #include<stack> #include<queue> #include<cctype> #include<sstream> using namespace std; #define pii pair<int,int> #define LL long long int const double eps=1e-10; const int INF=1000000000; const int maxn=7+3; int n,m,T; char Map[maxn][maxn]; int si,sj; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; int have[maxn][maxn]; bool dfs(int x,int y,int t) { int tx,ty; have[x][y]=0; bool ans=false; for(int i=0;i<4;i++) { tx=x+dx[i]; ty=y+dy[i]; if(tx>=0&&tx<n&&ty>=0&&ty<m) { if(Map[tx][ty]==‘D‘&&T==t+1) { return true; } else if(Map[tx][ty]==‘.‘&&have[tx][ty]==1) { ans=dfs(tx,ty,t+1); if(ans) return true; } } } have[x][y]=1; return false; } int main() { //freopen("in1.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d%d%d",&n,&m,&T)==3) { getchar(); if(n==0&&m==0&&T==0) break; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%c",&Map[i][j]); have[i][j]=1; if(Map[i][j]==‘S‘) { si=i;sj=j;//note the starting point } } getchar(); } /*for(int i=0;i<n;i++) { puts(Map[i]); }*/ //printf("%d %d\n",si,sj); if(dfs(si,sj,0)) { puts("YES"); } else puts("NO"); } //fclose(stdin); //fclose(stdout); return 0; }
标签:
原文地址:http://www.cnblogs.com/zywscq/p/4819985.html