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

news/2025/1/16 3:36:04/

题目

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.


http://www.ppmy.cn/news/648589.html

相关文章

webpack多页应用架构 - webpack的进阶应用

如何打造一个自定义的bootstrap&#xff1f; 前言 一般我们用bootstrap呐&#xff0c;都是用的从官网或github下载下来build好了的版本&#xff0c;千人一脸呐多没意思。当然&#xff0c;官网也给我们提供了自定义的工具&#xff0c;如下图所示&#xff0c;但每次要改些什么就要…

动画自动滚动div/像素基础知识/手机端样式选择/

大体上和原网页差不多&#xff0c;一个主页和一个子页面 动画自动滚动div&#xff1a; <!DOCTYPE html> <html> <head> <meta charset"UTF-8"> <title></title> <style type"text/css"> *{ margin: 0px; paddin…

2015年TCL i709M最新评测报导;5英寸机身背部镜面设计

【PingCe360】千元四核手机市场是今年各个国内厂商必争的一块”蛋糕“&#xff0c;TCL在2014年底不断发力&#xff0c;不仅推出了么么哒3N&#xff0c;还推出了多款新品&#xff0c;越来越开始注重外观设计。在硬件同质化的时代&#xff0c;或许外观才是制胜的法宝。近期推出的…

联通提速双4G战略:明年终端销量过亿

中国联通今日在四川成都发布双4G新计划&#xff0c;计划2015年销售1亿台4G终端&#xff0c;对4G终端提供50亿元专项补贴。 今日在四川成都召开的中国联通4G产业链高峰论坛上&#xff0c;中国联通常小兵董事长表示&#xff0c;在获得4G牌照后&#xff0c;尤其是在获批LTE混合组网…

学习资料网址

翻墙网站 http://www.ffbbb.cn/ http://www.qjqf.com/?p2367 http://blog.itpub.net/9240380/viewspace-757729 中央仓库 http://mvnrepository.com http://search.maven.org/ eclipse注释模版 http://www.cnblogs.com/senzjx/archive/2009/09/21/1570950.html aop日志操作…

[LC] 307. Range Sum Query - Mutable

这一题比起来304https://blog.csdn.net/chaochen1407/article/details/86572593就难多了。这一题首先是一维数组&#xff0c;那题是二维的。但是这题加入了一个情况就是有一个update的api可以让你更新数组的内容。在这个情况下原来那个办法就没有用了&#xff0c;因为原来的办法…

强行刷段位第四天

今天上来就是一道通过二元数组建立二叉树然后DFS和BFS的题。 忘了不说。。。也有点复杂。。。 看来今天是刷不完递归了 递归第一题&#xff1a; 题目&#xff1a; 给出一个二叉树&#xff0c;输出它的最大宽度和高度。 输入描述&#xff1a; 第一行一个整数n。 下面n行每行有…

jquery从零开始-2.2 结构选择器(一)

接上一节 jquery从零开始-2.1 jQuery 选择器基础 结构选择器就是根据 HTML 文档结构中节点之间的包含或并列关系决定匹配元素的一种方法。 jQuery 模仿 css 的关系过滤模式定义了 4 个关系选择器&#xff0c; 同时还根据包含关系&#xff0c;自定义了 4 个子元素选 择器。 1、…