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.
Sample test(s)
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
题目大意:3∗(N<=100)的网格,有个人要逃跑,他每次先向右移动一格,然后可以上下一格或者不动,然后车移动2格
问能否从右边界逃跑
bfs就好了,转移就向右,然后上下不动三种,车移动2格,可以根据相对运动,人直接再右移动两格就好了,这两格都要判
老规矩贴代码
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, k, sx;
char a[5][105];
typedef pair<int, int> P;
bool bfs(int sx) {
queue<P> q; q.push(P(sx, 1));
while(q.size()) {
int x = q.front().first, y = q.front().second; q.pop();
if(isalpha(a[x][++y])) continue;
if(y >= n) return true;
for(int i = -1; i <= 1; ++i) {
int nx = x + i;
if(nx < 1 || nx > 3) continue;
if(isalpha(a[nx][y]) || isalpha(a[nx][y + 1])
|| isalpha(a[nx][y + 2]) || a[nx][y + 2] == 1) continue;
int ny = y + 2;
// pr(nx); prln(ny);
if(ny >= n) return true;
a[nx][ny] = 1;
q.push(P(nx, ny));
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &k);
memset(a, 0, sizeof a);
for(int i = 1; i <= 3; ++i) {
scanf("%s", a[i] + 1);
if(a[i][1] == 's') sx = i;
}
puts(bfs(sx) ? "YES" : "NO");
}
return 0;
}