提示優先左走即:若是左邊能走就向左走,若是不能走向前走,若是前不能走,向右走,若是右不能走向後走。node
思路:若是能夠向左走就會一直向左走,用深搜,按優先順序搜索(代碼就很bad ,hahahah)。ios
#include<map> #include<stack> #include<queue> #include<math.h> #include<vector> #include<string> #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #define mem(a,b) memset(a,b,sizeof(a)) #define maxn 105 #define maxm 100005 #define mod 1000000007 #define ll long long #define inf 0x3f3f3f3f using namespace std; string s[maxn]; struct node{ int x,y; }src,last; int n,m; int vis[10][maxn][maxn]; int check(char a){ if(a=='N')return 0; if(a=='W')return 1; if(a=='S')return 2; if(a=='E')return 3; } bool judge(int x,int y){ if(x>=0&&x<n&&y>=0&&y<m&&s[x][y]!='#') return true; return false; } int dfs(int x,int y,char d){ char t;int dir; if(x==last.x&&y==last.y)return 1; if(d=='N'){ if(judge(x,y-1)){t='W';dir=check(t); if(vis[dir][x][y-1]==1)return 0;else{vis[dir][x][y-1]=1; return dfs(x,y-1,t);}} else if(judge(x-1,y)){t='N';dir=check(t); if(vis[dir][x-1][y]==1)return 0;else{vis[dir][x-1][y]=1; return dfs(x-1,y,t);}} else if(judge(x,y+1)){t='E';dir=check(t); if(vis[dir][x][y+1]==1)return 0;else{vis[dir][x][y+1]=1 ;return dfs(x,y+1,t);}} else if(judge(x+1,y)){t='S';dir=check(t); if(vis[dir][x+1][y]==1)return 0;else{vis[dir][x+1][y]=1; return dfs(x+1,y,t);}} else return 0; } if(d=='W'){ if(judge(x+1,y)){t='S';dir=check(t); if(vis[dir][x+1][y]==1)return 0;else{vis[dir][x+1][y]=1; return dfs(x+1,y,t);}} else if(judge(x,y-1)){t='W';dir=check(t); if(vis[dir][x][y-1]==1)return 0;else{vis[dir][x][y-1]=1; return dfs(x,y-1,t);}} else if(judge(x-1,y)){t='N';dir=check(t); if(vis[dir][x-1][y]==1)return 0;else{vis[dir][x-1][y]=1; return dfs(x-1,y,t);}} else if(judge(x,y+1)){t='E';dir=check(t); if(vis[dir][x][y+1]==1)return 0;else{vis[dir][x][y+1]=1; return dfs(x,y+1,t);}} else return 0; } if(d=='S'){ if(judge(x,y+1)){t='E';dir=check(t); if(vis[dir][x][y+1]==1)return 0;else{vis[dir][x][y+1]=1; return dfs(x,y+1,t);}} else if(judge(x+1,y)){t='S';dir=check(t); if(vis[dir][x+1][y]==1)return 0;else{vis[dir][x+1][y]=1; return dfs(x+1,y,t);}} else if(judge(x,y-1)){t='W';dir=check(t); if(vis[dir][x][y-1]==1)return 0;else {vis[dir][x][y-1]=1;return dfs(x,y-1,t);}} else if(judge(x-1,y)){t='N';dir=check(t); if(vis[dir][x-1][y]==1)return 0;else {vis[dir][x-1][y]=1;return dfs(x-1,y,t);}} else return 0; } if(d=='E'){ if(judge(x-1,y)){t='N';dir=check(t); if(vis[dir][x-1][y]==1)return 0;else {vis[dir][x-1][y]=1;return dfs(x-1,y,t);}} else if(judge(x,y+1)){t='E';dir=check(t); if(vis[dir][x][y+1]==1)return 0;else {vis[dir][x][y+1]=1;return dfs(x,y+1,t);}} else if(judge(x+1,y)){t='S';dir=check(t); if(vis[dir][x+1][y]==1)return 0;else {vis[dir][x+1][y]=1;return dfs(x+1,y,t);}} else if(judge(x,y-1)){t='W';dir=check(t); if(vis[dir][x][y-1]==1)return 0;else {vis[dir][x][y-1]=1;return dfs(x,y-1,t);}} else return 0; } } int main(){ while(~scanf("%d%d",&n,&m)){ mem(vis,0); for(int i=0;i<n;i++){ cin>>s[i]; for(int j=0;j<m;j++){ if(s[i][j]=='T'){src.x=i;src.y=j;} if(s[i][j]=='X'){last.x=i;last.y=j;} } } char dir;cin>>dir; if(dfs(src.x,src.y,dir)==1) printf("YES\n"); else printf("NO\n"); } }