Phillip and Trains
Input
Each test contains from one to ten sets of the input data. The first line of the test contains a single integert (1 ≤ t ≤ 10 for pretests and tests ort = 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 ofn 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 thek 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 Input
2 16 4 ...AAAAA........ s.BBB......CCCCC ........DDDDD... 16 4 ...AAAAA........ s.BBB....CCCCC.. .......DDDDD....
YES NO
2 10 4 s.ZZ...... .....AAABB .YYYYYY... 10 4 s.ZZ...... ....AAAABB .YYYYYY...
YES NO
Sample Output
题意:
人在最左边开始以1米每秒的速度往右走,火车以2米每秒的速度往左走
为了方便处理问题可以认为人和火车交替走
人先右走一个格子,然后选择往上、 往下、 不动。
然后火车走
解题思路
数据量小可以dfs
相当于在合法的情况下人 选择上 中 下 走三步
枚举选择即可
代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,m;///隧道长n,m个车
char mmap[3][200];
int ans = 0;
int vis[3][200];
void solve(int x,int y){///当前位置
if(y>=n-1) {ans = 1;return;}
vis[x][y] = 1;
if(mmap[x][y+1]!='.') return;
for(int i =-1;i<2;++i){
if(x+i<0 || x+i>3 || mmap[x+i][y+1]!='.') continue;
int f=1;
for(int j=1;j<=3;++j){
if(mmap[x+i][y+j]!='.'){
f=0;
break;
}
}
if(!f) continue;
if(!vis[x+i][y+3])
solve(x+i,y+3);
}
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
int sx;
ans = 0;
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&m);
for(int i=0;i<3;++i){///地图的边界是0-n-1
scanf("%s",mmap[i]);
}
for(int i=0;i<3;++i)
for(int j = n;j<=n+10;++j)
mmap[i][j]='.';
for(int i=0;i<3;++i){
if(mmap[i][0]=='s'){
sx=i;
}
}
solve(sx,0);
if(!ans) printf("NO\n");
else printf("YES\n");
}
return 0;
}