Mystic Falls is a mysterious place with vampires, werewolves, wizards, doppelgangers. Klaus (vampire ancestor) also came to Mystic Falls in order to use Elena 's blood to develop his half-blood army (vampire & werewolf). Because Stefan loves Elena deeply , Stefan decides to wake up the vampire hunter to rescue Elena .
The vampire hunter is locked in a maze that has the property of being disoriented as soon as you enter it. So Stefan thought of a way to take the left as the standard (that is, go left first), then go forward, go right, and if you can't go, go backward (that is, turn right twice). He can move one space up, down, left, and right, and it takes 1 minute each time. After learning that you are a gifted programmer, Stefan decides to let you judge whether he can find a vampire hunter.
enter:
The input contains multiple sets of test data. The first line is input n , m ( 2 < n , m <= 100) , the next n lines are m characters per line, and the n + 2 line is a character that represents the initial direction (EWSN). "." stands for space, "#" for wall, "T" for initial position, and "X" for vampire hunter position.
output:
Output a line, if found, output "YES", otherwise output "NO".
Sample
4 4 .... .##. .##. TX.. N 4 4 .... .##. .### T#.X N
YES NO
I wrote it by search. However, the first time I wrote a question with directions, I was a little excited. I ignored the situation that the same point can be walked in different directions. Later, I thought about using a 3-dimensional array to mark it.
#include<stdio.h> #include<string.h> #include<queue> #include<iostream> #include<algorithm> using namespace std; #define ll long long char folder [1005] [1005]; char s[10]; int v[1005][1005][10]; int dir[4][2]={0,-1,-1,0,0,1,1,0}; int n,m; int x1,yy1,x2,y2; int flag; int judge(int x3,int y3) { if(mapp[x3][y3]!='#'&&x3>=0&&x3<n&&y3>=0&&y3<m) return 1; return 0; } void dfs(int x,int y,int z) { if(flag!=0) return; if(x==x2&&y==y2) { flag=1; return ; } if(v[x][y][z]==1) { flag=-1; return ; } v[x][y][z]=1; int xx,yy,kk,i; for(i=0; i<4; i++) { kk=(z+i+3)%4;//Make sure to go left first xx=x+dir[kk][0]; yy=y+dir[kk][1]; if(judge(xx,yy)) break; } if(i!=4) dfs(xx,yy,kk); else { flag=-1; return ; } } intmain() { while(~scanf("%d%d",&n,&m)) { for(int i=0; i<n; i++) scanf("%s",mapp[i]); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(mapp[i][j]=='T') { x1=i,yy1=j; } if(mapp[i][j]=='X') { x2=i,y2=j; } } } flag=0; memset(v,0,sizeof(v)); scanf("%s",s); if(s[0]=='W') dfs(x1,yy1,0); else if(s[0]=='N') dfs(x1,yy1,1); else if(s[0]=='E') dfs(x1,yy1,2); else dfs(x1,yy1,3); if(flag==1) printf("YES\n"); else printf("NO\n"); } }