您的位置:首页 > 大数据 > 人工智能

CodeForces 586D Phillip and Trains (基础搜索 -- DFS)

2016-10-25 23:18 411 查看
题目链接:点击打开链接
D. Phillip and Trains

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Examples

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


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.

题目大意:这道题的模型就是前一阵(或者说现在也还是)很流行的(地铁)跑酷游戏几乎是一样的,大意就是一个人在一个有三条轨道的隧道里跑,火车以一定的速度向他开过来,他在想办法走出隧道的过程中要不断躲避火车,问他是否能成功走出隧道。

解题思路:题目中人每次向前走一个单位然后可以选择原地不动或者变道;火车每次向前走两个单位,两者相向。每次处理如果要真的让火车的位置改变显然不现实。其实很容易看出来每次人相对火车走三个单位,这样只要处理人就行了。有一点要注意的是人是先向相对火车的方向走一步然后才能选择变道或者不动,这里需要特判一下。然后扩展一下地图右边界(隧道出口的边界),以免数组溢出就行了。

解题过程:这道题是个不太难的搜索,简单DFS就行。代码如下,搜索的思路参考了一下网址(博客)的思路:点击打开链接

<span style="font-family:Comic Sans MS;font-size:12px;">#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

char tun[3][110];
int vis[3][110];
int n,k;
bool flag; //判断是否能成功走出隧道的标志

bool inMap(int x)
{
if(x>=0 && x<=2)
return true;
return false;
}

void dfs(int x,int y)
{
vis[x][y]=1;
if(y>=n)
{
flag=true;
return ;
}
if(tun[x][y+1]!='.')
return ;
if(inMap(x-1) && !vis[x-1][y+3])
{
if(tun[x-1][y+1]=='.' && tun[x-1][y+2]=='.' && tun[x-1][y+3]=='.')
dfs(x-1,y+3);
}
if(inMap(x+1) && !vis[x+1][y+3])
{
if(tun[x+1][y+1]=='.' && tun[x+1][y+2]=='.' && tun[x+1][y+3]=='.')
dfs(x+1,y+3);
}
if(!vis[x][y+3])
{
if(tun[x][y+1]=='.' && tun[x][y+2]=='.' && tun[x][y+3]=='.')
dfs(x,y+3);
}
}

int main()
{
int t;
cin >> t;
while(t--)
{
cin >> n >> k;
memset(vis,0,sizeof(vis));
for(int i=0; i<3; i++)
cin >> tun[i];
for(int i=0; i<3; i++)
{
for(int j=n; j<n+5; j++)
tun[i][j]='.';
}
flag=false;
for(int i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
if(tun[i][j]=='s')
{
dfs(i,j);
break;
}
}
}
if(flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}</span>

82c3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息