Codeforces 585B - Phillip and Trains

Description

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 Input

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

Hint

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列,m个火车(火车用不同的大写字母来表示,任意长度)

有一个人起点在最左一列,用小写字母s表示,运动规则如下:

①: 这个人首先向右移动一格(在右边一格不是字母的情况下)

②: 其次可以选择向上、向下、不动任意一种方式移动(当然,移动至的位置不能是字母),当这个人选择一种方式移动后,所有的火车会全部同时向前移动两格,如果火车到达左边界,那么火车会继续向前行驶

判断这个人是否可以到达轨道的最右边,即最后一列

 

思路:DFS

在进行移动的时候,由于火车也需要向前移动两格,但是不好去控制火车的坐标

所以选择改变人的坐标,火车向左移动两格,即代表人向右移动两格,那么就改变y坐标,DFS(x,y+2)


①: 判断此时的y是否已经超过边界值(条件 y>=n ,不只是等于n,因为每次y的变化是增加2)

②: 判断右边一格是否为 ' . ' ,如果不是直接结束DFS

③: 进行上下左边变化得到新的坐标 (p,q) ,判断q的位置

   如果q已经等于边界值,则需要判断当前点 (x,y) 的字符,如果当前点是 ' . ' ,说明已经到达边界,则结束DFS

  

例如这组样例

2 1
s.
AA
..

答案是 YES

   如果q需要一步才到达边界,那么需要判断(p,q) 与 (p,q+1)是否为 ' . ‘

               如果不是的话,只能选择不动,不动的条件需要判断(x,y) 与 (x,y+1) 是否为 ' . '

   如果q需要两步才到达边界,那么需要判断(p,q) 与 (p,q+1) 与 (p,q+2) 是否为 ' . ‘

                                                      如果不是的话,只能选择不动,不动的条件需要判断(x,y) 与 (x,y+1) 与 (x,y+2) 是否为 ' . '

        同理需要三步以上才到达边界的话,条件也可以得出

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXX = 3e3+10;
char c[4][110];
int xx[] = {1,-1};
int yy[] = {0,0};
int n,flag;

void DFS(int x,int y)
{
    int sum = 0;
    int p,q;
    if(y >= n){
        flag = 1;
        return;
    }
    if(c[x][y+1] != '.')
        return;
    y+=1;

    for(int i=0; i<2; i++){
        p = x + xx[i];
        q = y + yy[i];
        if(p >= 1 && p <= 3 && q >= 1 && q <= n)
        {
            if(q + 3 <= n){
                if(c[p][q+1] == '.' && c[p][q+2] == '.' && c[p][q+3] == '.' && c[p][q] == '.')
                    DFS(p,q+2);
                else if(c[x][y+1] == '.' && c[x][y+2] == '.' && c[x][y+3] == '.' && c[x][y] == '.')
                    DFS(x,y+2);
            } else if(q + 2 <= n && c[p][q] == '.'){
                if(c[p][q+1] == '.' && c[p][q+2] == '.' && c[p][q] == '.')
                    DFS(p,q+2);
                else if(c[x][y+1] == '.' && c[x][y+2] == '.' && c[x][y] == '.')
                    DFS(x,y+2);
            } else if(q + 1 <= n){
                if(c[p][q+1] == '.' && c[p][q] == '.')
                    DFS(p,q+2);
                if(c[x][y+1] == '.' && c[x][y] == '.')
                    DFS(x,y+2);
            } else if(q == n && c[x][y] == '.'){
                flag = 1;
                return;
            }
        }
    }
}

int main()
{
    int t,m,x,y;
    cin >> t;
    while(t--){
        memset(c,0);
        cin >> n >> m;
        for(int i=1; i<=3; i++){
            for(int j=1; j<=n; j++){
                cin >> c[i][j];
                if(c[i][j] == 's')
                    x = i,y = j;
            }
        }

        flag = 0;
        DFS(x,y);
        if(flag == 1)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值