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

题目

D. Phillip and Trains

Background
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)
Case1: Input
2
16 4
…AAAAA……..
s.BBB……CCCCC
……..DDDDD…
16 4
…AAAAA……..
s.BBB….CCCCC..
…….DDDDD….
Case1: Output
YES
NO

Case2: Input
2
10 4
s.ZZ……
…..AAABB
.YYYYYY…
10 4
s.ZZ……
….AAAABB
.YYYYYY…
Case2: Output
YES
NO

题目大意

一个 3n 的矩阵中有一个英雄和一堆火车
每次移动中,英雄先向右移动一格,然后可以选择向上,向下或原地不动,然后所有火车向左移动两个
英雄不能卧轨
问英雄能不能到达矩阵的最右端,火车不会再次出现

解题思路

由于只有三行,状态并不多,所以可以宽搜
这时候考虑物理中的相对运动思想,火车向左两格,就相当于英雄向右两格。所以英雄的运动就可以“分解”为先向右一格,再向上或向下或不动,再向右两个,而火车是不动的。在运动中的任意一步,英雄都不能碰到火车
然后解决终止状态的问题。在原问题中,英雄每次向右一格,要走 n1 步,到达第n列。转化之即为,新问题中,每次向右三格,走 n1 步,到达第 3n2
这样宽搜时的状态就被大大简化,只需要记录英雄当前所在的点
注意一点,在转移过程中已经在队列里的点不应被再一次转移到

代码

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define red(i, a, b) for(int i = (a); i >= (b); i--)
#define ll long long
#define PII pair<int, int>

inline int read() {
    char c = getchar(); int x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c)) {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

const int N = 500;
char str[N];
queue<PII> q;
int T, len, K;
int a[10][N];
int vis[10][N];

int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &len, &K);
        int OK = 0;
        while(!q.empty()) q.pop();
        memset(a, 0, sizeof(a));
        memset(vis, 0, sizeof(vis));
        rep(col, 1, 3) {
            scanf("%s", str);
            rep(i, 0, len - 1) {
                if (str[i] == 's') {
                    q.push(make_pair(col, i + 1));
                    a[col][i + 1] = 0;
                    vis[col][i + 1] = 1;
                }
                else if (str[i] == '.') a[col][i + 1] = 0;
                else a[col][i + 1] = 1;
            }
        }
        while(!q.empty()) {
            PII now = q.front();
            q.pop();
            int inow = now.first, jnow =now.second;
            if (jnow >= len * 3 - 2) { OK = 1; break; }
            if (a[inow][jnow + 1]) continue;
            rep(i, 1, 3) {
                if (fabs(i - inow) >= 2) continue;
                if (a[i][jnow + 1]) continue;
                if (a[i][jnow + 2]) continue;
                if (a[i][jnow + 3]) continue;
                if (vis[i][jnow + 3]) continue; //不能转移到已经在队列中的点
                vis[i][jnow + 3] = 1;
                q.push(make_pair(i, jnow + 3));
            }
        }
        if (OK) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

尾声

相对运动和参考系的想法来自物(H)竞(B)男(H)神
然而这题其他人都爆炸了,我侥幸存活
我爱物理么么哒
结果这场炸了C题,只涨了6Rating
我选择狗带

End.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值