zoukankan      html  css  js  c++  java
  • Codeforces 586D. Phillip and Trains 搜索

    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.

     题目链接:http://codeforces.com/contest/586/problem/D


    题意:人每秒往右走一步,然后向上一行或者向下一行后者保持在这一行;车每秒往左走2步。人先走,车再走。求人能不能从左走到右。

    思路:BFS。人相对车来说就是每秒往右走3步。人先向右走一步,,然后然后向上一行或者向下一行后者保持在这一行,然后向右走2步,这里每走一步都要判断是否可行。

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 using namespace std;
     7 const int MAXN=1e2+100;
     8 int n;
     9 int sign[5][MAXN];
    10 char edge[5][MAXN];
    11 queue<pair<int,int> >q;
    12 int dir[3]= {0,-1,1};
    13 int BFS(int si,int sj)
    14 {
    15     while(!q.empty()) q.pop();
    16     sign[si][sj]=1;
    17     q.push(make_pair(si,sj));
    18     while(!q.empty())
    19     {
    20 
    21         int x=q.front().first,y=q.front().second;
    22         q.pop();
    23         int fx=x,fy=y+1;
    24         if(fy>=n-1) return true;
    25         if(edge[fx][fy]!='.') continue;
    26         for(int i=0; i<3; i++)
    27         {
    28             fx=x+dir[i];
    29             if(!(0<=fx&&fx<3&&edge[fx][fy]=='.')) continue;
    30             if(fy+2==n&&edge[fx][fy+1]=='.') return true;
    31             else if(fy+2<n&&edge[fx][fy+1]=='.'&&edge[fx][fy+2]=='.')
    32             {
    33                 if(sign[fx][fy+2]==0) q.push(make_pair(fx,fy+2));
    34                 sign[fx][fy+2]=1;
    35             }
    36         }
    37     }
    38     return false;
    39 }
    40 int main()
    41 {
    42     int T;
    43     scanf("%d",&T);
    44     while(T--)
    45     {
    46         int k;
    47         int si,sj;
    48         scanf("%d%d",&n,&k);
    49         getchar();
    50         for(int i=0; i<3; i++)
    51         {
    52             for(int j=0; j<n; j++)
    53             {
    54                 scanf("%c",&edge[i][j]);
    55                 if(edge[i][j]=='s') si=i,sj=j;
    56             }
    57             getchar();
    58         }
    59         memset(sign,0,sizeof(sign));
    60         if(BFS(si,sj)) cout<<"YES"<<endl;
    61         else cout<<"NO"<<endl;
    62     }
    63     return 0;
    64 }
    BFS
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    eclipse 报错问题:java.lang.ClassNotFoundException:
    java 根据Url下载对应的文件到指定位置,读txt文件获取url
    from表单校验插件 validate 实例
    登录注册校验方式
    微信浏览器发送ajax请求执行多次解决方法
    波哥博客Url
    常用ajax样例
    学习笔记......
    学习笔记...
    学习笔记..
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/6081735.html
Copyright © 2011-2022 走看看