Codeforces Round #325 (Div. 2) D bfs
連結:戳這裡
D. Phillip and Trains
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
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.
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.
Examples
input
2
16 4
...AAAAA........
s.BBB......CCCCC
........DDDDD...
16 4
...AAAAA........
s.BBB....CCCCC..
.......DDDDD....
output
YES
NO
input
2
10 4
s.ZZ......
.....AAABB
.YYYYYY...
10 4
s.ZZ......
....AAAABB
.YYYYYY...
output
YES
NO
Note
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.
題意:
給出三行字串,裡面有火車,還有一個英雄的初始位置(一定在最左邊的那一列)
英雄先像前右走一步,然後選擇像上走或者向下走或者不動,如果碰到火車就死了
思路:
直接bfs就好了,三個方向
程式碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
struct node{
int x,y;
node(int x=0,int y=0):x(x),y(y){}
};
int n,k;
int vis[4][110];
int xx[3]={-1,1,0};
int yy[3]={2,2,2};
bool pd(node t){
if(t.x<1 || t.x>3) return false;
if(vis[t.x][t.y]==1) return false;
return true;
}
queue<node> qu;
void BFS(node S){
while(!qu.empty()) qu.pop();
qu.push(S);
vis[S.x][S.y]=1;
while(!qu.empty()){
node now=qu.front(),next;
qu.pop();
now.y++;
if(vis[now.x][now.y]) continue;
for(int i=0;i<3;i++){
next.x=now.x+xx[i];
next.y=now.y+yy[i];
if(pd(next)){
int f=0;
if(i==0){
if(vis[now.x-1][now.y]) continue;
else f=1;
} else if(i==1){
if(vis[now.x+1][now.y]) continue;
else f=1;
} else f=1;
if(f==1){
qu.push(next);
vis[next.x][next.y]=1;
if(next.y>n){
printf("YES\n");
return ;
}
}
}
}
}
printf("NO\n");
}
int main(){
int T;
scanf("%d",&T);
while(T--){
string s[4];
mst(vis,0);
scanf("%d%d",&n,&k);
for(int i=1;i<=3;i++) {
cin>>s[i];
}
node S;
for(int i=1;i<=3;i++){
for(int j=0;j<s[i].size();j++){
if(s[i][j]=='s') S=node(i,j+1);
else if(s[i][j]=='.') vis[i][j+1]=0;
else vis[i][j+1]=1;
}
}
BFS(S);
}
return 0;
}
相關文章
- Codeforces Round #325 (Div. 2) B 遞推
- Codeforces Round #325 (Div. 2) C 模擬
- Codeforces Round #361 (Div. 2) B BFS最短路
- Codeforces Round #452 (Div. 2) D
- Codeforces Round 941 (Div. 2) D
- Codeforces Round 960 (Div. 2)(A - D)
- Codeforces Round #250 (Div. 2) A-D
- Codeforces Round #251 (Div. 2) A/B/D
- Codeforces Round #283 (Div. 2) D,E
- Codeforces Round #256 (Div. 2)A-D
- Codeforces Round #358 (Div. 2) D dp
- Codeforces Round #359 (Div. 2) D DFS
- Educational Codeforces Round 34 (Rated for Div. 2) D
- Codeforces Round #290 (Div. 2) A,B,C,D
- Codeforces Round #263 (Div. 2) A-D
- Codeforces Round 720 (Div. 2) A-D
- Codeforces Round #673 (Div. 2)(A-D)題解
- Codeforces Round #401 (Div. 2)(C,D,E)
- Codeforces Round #321 (Div. 2) D 狀壓dp
- Codeforces Round #360 (Div. 2) D 數學題
- Codeforces Round #288 (Div. 2) A,B,C,D,E
- Codeforces Round #287 (Div. 2)A,B,C,D,E
- Codeforces Round #253 (Div. 2) D. Andrey and Problem
- Codeforces Round #280 (Div. 2 A,B,C,D,E)
- Educational Codeforces Round 170 (Rated for Div. 2) A-D
- 【數論】Codeforces Round #404 (Div. 2)(D)Anton and School - 2
- Educational Codeforces Round 142 (Rated for Div. 2) A-D
- Codeforces Round #336 (Div. 2) D 區間dp
- Codeforces Round 960 (Div. 2) 補題記錄(A~D)
- CodeForces Round #951(Div. 2) 補題記錄(A~D)
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A