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.
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.
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.
题意:
Philip 要想安全穿越地铁(从左到右),地铁至少有2格长(不然列车左移2格要一格一格来)。
每次,Philip会先右移一格,然后选择,上移一格,下移一个,或停止不动。然后地铁左移2格。
思路:
以 地铁 为参考系,每次地铁左移2格,改成Philip右移2格,对Philip进行BFS,详见代码1。
或者使用三维数组存储每个时间的图,然后模拟 Philip 的移动(从左到右)保证移动合法即可,详见代码2.
代码1:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define For(a,b,c) for(int a = b; a <= c; a++)
char s[5][105];
int n, m, book[5][105];
struct Node
{
int x, y;
}st, ne;
bool judge(int x, int y)
{
if(x < 1 || x > 3) return false;
if(s[x][y] == '.' || s[x][y] == '\0' || s[x][y] == 's') return true;
return false;
}
bool bfs(int x, int y)
{
memset(book,0,sizeof(book));
st.x = x;
st.y = y;
queue<Node> q;
q.push(st);
while(!q.empty())
{
st = q.front();
q.pop();
if(st.y >= n) return true;
if( !judge(st.x, st.y+1) ) continue;
st.y++;
if( judge(st.x+1, st.y) ) //上移1
{
ne.x = st.x + 1;
ne.y = st.y + 2;
if( judge(ne.x,ne.y) && !book[ne.x][ne.y]++ ) q.push(ne); //右移2
}
if( judge(st.x-1, st.y) ) //下移1
{
ne.x = st.x - 1;
ne.y = st.y + 2;
if( judge(ne.x,ne.y) && !book[ne.x][ne.y]++ ) q.push(ne); //右移2
}
if( judge(st.x, st.y) ) //停止
{
ne.x = st.x;
ne.y = st.y + 2;
if( judge(ne.x,ne.y) && !book[ne.x][ne.y]++ ) q.push(ne); //右移2
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
n--;
scanf("%s %s %s",s[1],s[2],s[3]);
For(i,1,3)
{
For(j,0,n)
{
if(s[i][j] == 's')
{
if( bfs(i,j) ) printf("YES\n");
else printf("NO\n");
}
}
}
}
return 0;
}
代码2:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define For(a,b,c) for(int a = b; a <= c; a++)
//dp_sub[a][b][c],a代表行,b代表列,c代表时间,存储每个时刻对应所有列车位置
//dp_man[a][b],a代表行,b代表列,存储Philip可到达点
int dp_sub[3][105][105], dp_man[3][105];
char s[3][105];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n, k;
scanf("%d%d",&n,&k);
n--;
memset(dp_sub,0,sizeof(dp_sub));
memset(dp_man,0,sizeof(dp_man));
scanf("%s%s%s",s[0],s[1],s[2]);
For(i,0,2)
For(j,0,n)
if( s[i][j] >= 'A' && s[i][j] <= 'Z' )
for(int a = j, t = 0; a >= 0; a -= 2, t++)
dp_sub[i][a][t] = 1;
For(i,0,2)
For(j,0,n)
if(s[i][j] == 's') dp_man[i][j] = 1;
For(i,0,n)//从左向右dp
{
For(j,0,2)
{
if(dp_man[j][i])
{
if(dp_sub[j][i+1][i]) continue; //先判断人右移一位是否有车
For(a,0,2)
{
//第一个判断保证移动一格,后面两个判断保证不会被车撞死
if( abs(a-j) < 2 && !dp_sub[a][i+1][i] && !dp_sub[a][i+1][i+1] )
dp_man[a][i+1] = 1;
}
}
}
}
int flag = 0;
For(i,0,2) flag += dp_man[i][n];
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}