Codeforces Round #325 (Div. 2) D. Phillip and Trains BFS
D. Phillip and Trains
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/586/problem/DDescription
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.
![](http://codeforces.com/predownloaded/d5/a6/d5a6065888197a9ea515f7100d816fb1ed501286.png)
Input
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.
,Ci,即此题的初始分值、每分钟减少的分值、dxy做这道题需要花费的时间。Output
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.
Sample Input
2
16 4
...AAAAA........
s.BBB......CCCCC
........DDDDD...
16 4
...AAAAA........
s.BBB....CCCCC..
.......DDDDD....
Sample Output
YES
NO
HINT
题意
在一个n*3的格子中有火车和人,人一开始在最左边,人的目标是走到最右边
每次人先往右走一步,然后可以选择往上,往下,或者不动。然后火车前进两格
要求火车和人不相撞,然后问你这个人可不可以走到最右边
题解:
我BFS的,因为火车每次得走两步,所以最多就50个图,我预先把50个图全部预处理出来
然后就直接bfs跑就好了
代码:
#include<stdio.h> #include<iostream> #include<cstring> #include<queue> using namespace std; string s[5]; int Mp[300][4][205]; int dy[5]={0,1,-1}; int vis[300][300]; struct node { int x,y,t; }; queue<node> Q; int main() { int t;scanf("%d",&t); while(t--) { memset(Mp,0,sizeof(Mp)); memset(vis,0,sizeof(vis)); while(!Q.empty())Q.pop(); int a,b; scanf("%d%d",&a,&b); for(int i=0;i<3;i++) cin>>s[i]; int len=s[0].size(); node now; for(int i=0;i<3;i++) { for(int j=0;j<len;j++) { if(s[i][j]<='Z'&&s[i][j]>='A') Mp[j][i][0]=1; if(s[i][j]=='s') { now.x = j; now.y = i; now.t = 0; } } } int TTT= 0; for(int i=1;i<=60;i++) { for(int j=0;j<3;j++) { for(int k=0;k<len;k++) { if(Mp[k][j][i-1]==1) { if(k-2>=0) { TTT = i; Mp[k-2][j][i]=1; } } } } } int flag = 1; Q.push(now); while(!Q.empty()) { now = Q.front(); Q.pop(); if(now.x>=len-1||now.t>=TTT) { printf("YES\n"); flag = 0; break; } for(int i=0;i<3;i++) { node next = now; next.x += 1; if(Mp[next.x][next.y][next.t]) continue; next.y += dy[i]; next.t = next.t+1; if(next.y<0||next.y>=3) continue; if(Mp[next.x][next.y][next.t-1]||Mp[next.x][next.y][next.t]) continue; if(vis[next.x][next.y])continue; vis[next.x][next.y]=1; Q.push(next); } } if(flag==1) { printf("NO\n"); } } }