Codeforces Round #325 (Div. 2) D. Phillip and Trains (BFS)

D. Phillip and Trains
 

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.

题意是人和火车轮流移动,一回合里人要先向右移动一格,然后再向下移动一格或向上移动一格或不移动,然后火车可以向左移动两格。撞(chuang)上,人就挂了。求人能不能到最右边。

bfs一下,要注意的是,人不是直接到右上或者右下的,所以,我们要先检查能不能向右移动,再检查向上或向下的情况。贴心的是样例里就有这种情况,就很开心的1A了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
char s[5][110];
int n, k;
struct node {
    int x, y, t;
};

int dx[] = {1, 1, 1}, dy[]= {1, -1, 0};
int vis[5][110];

bool check(node a, int flag) {
    int i = 1;
    char pre = '.';
    for(int i = 1; i <= n; i++) {
        char c = s[a.y][i];
        if(c >= 'A' && c <= 'Z') {
            //printf("%c %d\n", c, a.t);
            if(i - 2 * (a.t - 1) == a.x || (i - 2 * (a.t) == a.x && flag == 2)) return false;
        }
    }
    return true;
}

void bfs(int x, int y) {
    memset(vis, 0, sizeof(vis));
    node now, nxt;
    now.x = x, now.y = y;
    now.t = 0;
    queue<node> q;
    q.push(now);
    while(!q.empty()) {
        now = q.front(); q.pop();
        if(now.x == n) {
            puts("YES");
            return;
        }
        for(int i = 0; i < 3; i++) {
            nxt.t = now.t + 1;
            nxt.x = now.x + dx[i];
            nxt.y = now.y;
            if(!check(nxt, 1)) continue;
            nxt.y = now.y + dy[i];
            int x = nxt.x, y = nxt.y;
            if(!vis[y][x] && check(nxt, 2) && y >= 1 && y <= 3 && x >= 1 && x <= n) {
                vis[y][x] = 1;
                //printf("%d %d %d\n", x, y, nxt.t);
                q.push(nxt);
            }
        }
    }
    puts("NO");
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &n, &k);
        for(int i = 1; i <= 3; i++) scanf("%s", s[i] + 1);
        for(int i = 1; i <= 3; i++) {
            if(s[i][1] == 's') {
                bfs(1, i);
                break;
            }
        }
    }
}

 

posted @ 2016-07-12 21:05  MartinEden  阅读(165)  评论(0编辑  收藏  举报