您的位置:首页 > 大数据 > 人工智能

CF Round #325 Div2 D. Phillip and Trains

2015-10-17 02:05 411 查看
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.

Sample test(s)

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 test set.

解析

BFS,大致做法和群主巨巨qscqesze的解法一样,唯一不同的是人和车相对运动,我看成车静止,人每次跑三格。简单的模拟一下就可以发现只有在3n+1列(从0开始计数,s开始在0列)的位置可以上下移动,其他时候都只能走直线。

MLE了一发,因为在搜索树里没有去掉相同状态。

#include<cstdio>
#include<string>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

struct Cood
{int r,c;};
int N,K;
string s[3];
bool hash[3][110];
queue<Cood> q;

void readdata()
{
scanf("%d%d",&N,&K);
for(int i=0;i<3;i++) cin >>s[i];
}

bool work()
{
while(q.size()) q.pop();
memset(hash,0,sizeof(hash));
for(int i=0;i<3;i++)
if(s[i][0]=='s')
{
if(s[i][1]=='.'){q.push((Cood){i,1});hash[i][1]=1;}
else return 0;
break;
}

while(q.size())
{
Cood k=q.front(); q.pop();
if(k.c>=N) return 1;
for(int i=-1;i<=1;i++)
{
Cood nt={k.r+i,k.c+3};
if(nt.r<0||2<nt.r||hash[nt.r][nt.c]) continue;
bool flag=0;
for(int j=k.c;j<=min(N-1,nt.c);j++)
if(s[nt.r][j]!='.') {flag=1;break;}
if(!flag)
{
q.push(nt);
hash[nt.r][nt.c]=1;
//printf("r:%d c:%d\n",nt.r,nt.c);
}
}
}
return 0;
}

int main()
{
int T; scanf("%d",&T);
while(T--)
{
readdata();
if(work())puts("YES");
else puts("NO");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: