Phillip and Trains CodeForces - 585B (DFS)

博主解析了Codeforces问题585B中地铁滚动者游戏的解法,强调了路径记忆的重要性,避免重复走过已知路段。通过精确定位和策略调整,确保主人公在有限时间内避开火车,达到终点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem - 585B - Codeforceshttps://codeforces.com/problemset/problem/585/B

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 1

InputcopyOutputcopy
2
16 4
...AAAAA........
s.BBB......CCCCC
........DDDDD...
16 4
...AAAAA........
s.BBB....CCCCC..
.......DDDDD....
YES
NO

Sample 2

InputcopyOutputcopy
2
10 4
s.ZZ......
.....AAABB
.YYYYYY...
10 4
s.ZZ......
....AAAABB
.YYYYYY...
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.

#include <iostream>
using  namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<string.h>
#pragma warning (disable:4996)
#include<stack>
#include<set>
int t, n, k;
char graph[4][200];
bool flag = false;
void dfs(int x, int y);
int diry[] = { 0,1,-1 };
bool book[3][200];
int main() {
	cin >> t;
	for (int cnt = 0; cnt < t; cnt++) {
		flag = false;
		fill(graph[0], graph[0] + 4 * 200, '.');
		fill(book[0], book[0] + 3 * 200, false);
		cin >> n >> k;
		for (int cnt = 0; cnt < 3; cnt++)
			for (int cnt1 = 0; cnt1 < n; cnt1++)
				cin >> graph[cnt][cnt1];
		int y;
		for (int cnt = 0; cnt < 3; cnt++)
			if (graph[cnt][0] == 's')
				y = cnt, graph[cnt][0] = '.';
		dfs(0, y);
		if (flag)
			cout << "YES";
		else
			cout << "NO";
		cout << endl;
	}
	return 0;
}
void dfs(int x, int y) {
	if (book[y][x])
		return;
	if (y < 0 || y>2 || x < 0 || flag)
		return;
	if (graph[y][x] != '.')
		return;
	if (x >= n ) {
		flag = true;
		return;
	}
	book[y][x] = true;
	for (int cnt = 0; cnt < 3; cnt++) {
		int nex, ney;
		nex = x + 1;
		ney = y;
		if (graph[ney][nex] != '.')
			continue;
		ney += diry[cnt];
		if (graph[ney][nex] != '.')
			continue;
		nex += 1;
		if (graph[ney][nex] != '.')
			continue;
		nex += 1;
		if (graph[ney][nex] != '.')
			continue;
		dfs(nex, ney);
	}
}

 这道题第三个测试点卡了我很久,后面发现是我最初的时候不加book和flag的话,会有很多分支,导致时间复杂度直接飙升,,还有我自己最初return语句里面写x>=n,真tm愚蠢,因为我这里直接把图边界全部加了'.',根本不需要,加了反而会错,因为这样我的一些边界样例就过不去了,还有多个图遍历,一定要重置book,我tm真是吐了

然后这里为什么要记一下路线,而不是可能某条路线中途可以通后面不通,我最开始没想明白这个问题,所以我没有记忆路线,后面是因为超时,然后在网上看了一堆博客,还有这道题在cf上的解释,说是n的复杂度然后我自己又作了复杂度的分析,就发现我不是n,然后论证了为什么走过的路径不用走以后才写对了,下面是分析

首先,我们将每一次人物直走进行分割,会发现,其实无论在哪条路径上,人物的位置其实都是固定的,不会有因为先走了哪一步,导致后面的路径不同步,那么因为每一条路径上主人公其实都是在同一个竖格中,这就会有一个奇妙的性质,就是走过的路不用再走了,因为经过同步以后,这些路的每一个可能性其实已经固定住了,我们只需要拓展不知道的节点就好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cjz-lxg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值