CF div2 325 D
The mobile application store has a new game called "Subway Roller".
The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and n columns. At the beginning of the game the hero is in some cell of the leftmost column. Some number of trains rides towards the hero. Each train consists of two or more neighbouring cells in some row of the field.
All trains are moving from right to left at a speed of two cells per second, and the hero runs from left to right at the speed of one cell per second. For simplicity, the game is implemented so that the hero and the trains move in turns. First, the hero moves one cell to the right, then one square up or down, or stays idle. Then all the trains move twice simultaneously one cell to the left. Thus, in one move, Philip definitely makes a move to the right and can move up or down. If at any point, Philip is in the same cell with a train, he loses. If the train reaches the left column, it continues to move as before, leaving the tunnel.
Your task is to answer the question whether there is a sequence of movements of Philip, such that he would be able to get to the rightmost column.
Each test contains from one to ten sets of the input data. The first line of the test contains a single integer t (1 ≤ t ≤ 10 for pretests and tests or t = 1 for hacks; see the Notes section for details) — the number of sets.
Then follows the description of t sets of the input data.
The first line of the description of each set contains two integers n, k (2 ≤ n ≤ 100, 1 ≤ k ≤ 26) — the number of columns on the field and the number of trains. Each of the following three lines contains the sequence of n character, representing the row of the field where the game is on. Philip's initial position is marked as 's', he is in the leftmost column. Each of the k trains is marked by some sequence of identical uppercase letters of the English alphabet, located in one line. Distinct trains are represented by distinct letters. Character '.' represents an empty cell, that is, the cell that doesn't contain either Philip or the trains.
For each set of the input data print on a single line word YES, if it is possible to win the game and word NO otherwise.
2
16 4
...AAAAA........
s.BBB......CCCCC
........DDDDD...
16 4
...AAAAA........
s.BBB....CCCCC..
.......DDDDD....
YES
NO
2
10 4
s.ZZ......
.....AAABB
.YYYYYY...
10 4
s.ZZ......
....AAAABB
.YYYYYY...
YES
NO
In the first set of the input of the first sample Philip must first go forward and go down to the third row of the field, then go only forward, then go forward and climb to the second row, go forward again and go up to the first row. After that way no train blocks Philip's path, so he can go straight to the end of the tunnel.
Note that in this problem the challenges are restricted to tests that contain only one testset.
参考了一下别人的代码,然后弱渣重新打了一遍。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <queue> 5 #include <vector> 6 #include <stack> 7 8 using namespace std; 9 10 const int M = 10005; 11 const int maxn = 5000000; 12 typedef long long ll; 13 14 struct node 15 { 16 int x,y,t; 17 }; 18 queue<node>Q; 19 int vis[105][105][105],v[105][105]; 20 int dir[5][2] = {{0,1},{1,1},{-1,1}}; 21 char G[105][105]; 22 23 int n,k,sx,sy; 24 25 bool bfs(int x,int y,int t) 26 { 27 node p,q,z; 28 p.x = x; 29 p.y = y; 30 p.t = t; 31 Q.push(p); 32 while(!Q.empty()){ 33 q = Q.front(); 34 Q.pop(); 35 if(q.y == n) return true; 36 for(int i=0;i<3;i++){ 37 38 z.x = q.x + dir[i][0]; 39 z.y = q.y + dir[i][1]; 40 z.t = q.t; 41 if(z.x<=0||z.x>3||z.y<=0||z.y>n)continue; 42 if(vis[z.x][z.y][z.t]&&i==0)break; 43 if(!v[z.x][z.y]&&!vis[z.x][z.y][z.t]&&!vis[z.x][z.y][z.t+1]){ 44 v[z.x][z.y] = 1; 45 z.t = q.t+1; 46 Q.push(z); 47 } 48 } 49 } 50 return false ; 51 } 52 53 54 int main() 55 { 56 57 int t,T,i,j; 58 scanf("%d",&T); 59 while(T--) 60 { 61 while(!Q.empty()) 62 Q.pop(); 63 memset(vis,0,sizeof(vis)); 64 memset(v,0,sizeof(v)); 65 memset(G,0,sizeof(G)); 66 scanf("%d%d",&n,&k); 67 for(i=1;i<=3;i++) 68 for(j=1;j<=n;j++) 69 { 70 cin>>G[i][j]; 71 if(G[i][j]=='.')vis[i][j][0]=0; 72 else vis[i][j][0]=1; 73 if(G[i][j]=='s'){ 74 sx=i; 75 sy=j; 76 } 77 } 78 for(t=1;t<=100;t++) 79 { 80 for(i=1;i<=3;i++) 81 for(j=1;j<=n;j++) 82 { 83 if(vis[i][j][t-1]==1)vis[i][j-2][t]=1; 84 85 } 86 } 87 88 if(bfs(sx,sy,0))printf("YES\n"); 89 else printf("NO\n"); 90 } 91 92 return 0; 93 }