CF586D. Phillip and Trains
1 /* 2 CF586D. Phillip and Trains 3 http://codeforces.com/problemset/problem/586/D 4 搜索 5 */ 6 #include<cstdio> 7 #include<algorithm> 8 #include<string.h> 9 using namespace std; 10 const int Nmax=110; 11 char map[4][Nmax]; 12 int fail[4][Nmax]; 13 int n,k; 14 int t,ans; 15 16 int dfs(int step,int pos) 17 { 18 if(step==n-1 && map[pos][step+1]=='.') 19 { 20 ans=1; 21 return 1; 22 } 23 if(step>=n) 24 { 25 ans=1; 26 return 1; 27 } 28 if(ans) 29 return 1; 30 if(fail[pos][step]) 31 return 0; 32 33 34 if(map[pos][step]>='A' && map[pos][step]<='Z') 35 { 36 fail[pos][step]=1; 37 return 0; 38 } 39 for(int k=pos-1;k<=pos+1;k++) 40 { 41 if(k>=1 && k<=3 && map[pos][step+1]=='.' &&map[k][step+1]=='.' && !fail[k][step+1] && !fail[k][step+2] && !fail[k][step+3]) 42 { 43 if(map[k][step+2]=='.' && map[k][step+3]=='.') 44 { 45 if(dfs(step+3,k)) 46 { 47 ans=1; 48 return 1; 49 } 50 else 51 { 52 fail[k][step+3]=1; 53 } 54 } 55 56 } 57 } 58 fail[pos][step]=1; 59 return 0; 60 } 61 62 int main() 63 { 64 scanf("%d",&t); 65 while(t--) 66 { 67 scanf("%d%d",&n,&k); 68 for(int i=1;i<=3;i++) 69 for(int j=1;j<=n+10;j++) 70 fail[i][j]=0; 71 getchar(); 72 gets(map[1]+1); 73 gets(map[2]+1); 74 gets(map[3]+1); 75 for(int i=1;i<=3;i++) 76 for(int j=n+1;j<=n+10;j++) 77 map[i][j]='.'; 78 ans=0; 79 int pos=0,start; 80 for(int i=1;i<=3;i++) 81 if(map[i][1]=='s') 82 pos=i; 83 84 if(dfs(1,pos)) 85 printf("YES\n"); 86 else 87 printf("NO\n"); 88 } 89 return 0; 90 }