CodeForces 586D Phillip and Trains -dfs

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

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

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值