1 second
256 megabytes
standard input
standard 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.
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.
题意 :给你一个3*n的矩阵 不同的字母代表一列火车 如图很明显 '.'代表可以通过的路 ‘s’为人的初始点 人向右走每秒1格(改变方向不记入格数) 之后火车向左开两格 判断人是否能安全的通过边界
题解:bfs 注意边界处理
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int t;
char mp[][];
int dis[][]={{-,},{,},{,}};
int n,k;
struct node
{
int xx,yy;
};
int biao[][];
queue<node> q;
void bfs(int s,int e)
{
node exm,now;
while(!q.empty())
q.pop();
exm.xx=s;
exm.yy=e;
biao[s][e]=;
q.push(exm);
int flag=;
while(!q.empty())
{
exm=q.front();
q.pop();
if(exm.yy>n-)
{
flag=;
break;
}
for(int i=;i<;i++)
{
int xxx=exm.xx+dis[i][];
int yyy=exm.yy+dis[i][];
if(xxx>=&&xxx<&&yyy>=&&yyy<n+&&mp[xxx][yyy]=='.')
{
if(mp[exm.xx][exm.yy+]=='.'&&mp[xxx][exm.yy+]=='.'&&mp[xxx][exm.yy+]=='.')//判断通过的路径上都为‘.’
{
if(biao[xxx][yyy]==){
now.xx=xxx;
now.yy=yyy;
q.push(now);
biao[xxx][yyy]=;
}
}
}
}
}
if(flag==)
printf("YES\n");
else
printf("NO\n");
}
int main()
{
scanf("%d",&t);
for(int i=;i<=t;i++)
{
memset(mp,,sizeof(mp));
memset(biao,,sizeof(biao));
scanf("%d %d",&n,&k);
for(int j=;j<;j++)
scanf("%s",mp[j]);
mp[][n]='.';mp[][n+]='.';mp[][n+]='.';
mp[][n]='.';mp[][n+]='.';mp[][n+]='.';
mp[][n]='.';mp[][n+]='.';mp[][n+]='.';
int sx,sy;
for(int j=;j<;j++)
{
if(mp[j][]=='s'){
sx=j;
sy=;
}
}
bfs(sx,sy);
}
return ;
}