zoukankan      html  css  js  c++  java
  • Codeforces Round #325 (Div. 2)D. Phillip and Trains BFS

                                                 D. Phillip and Trains
     

    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.

    Sample test(s)
    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.

    题意:给你一个3*n的图,开始人在最左边第一列某个位置,每次人必须向右走一格,然后向下或者向上,不动一格,人走完,所有火车都从右向左走两格,问你人是否能走到最右边这列

    题解:就是人走完后再向右移两格的意思了,bfs就好了

    ///1085422276
    #include<bits/stdc++.h>
    using namespace std ;
    typedef long long ll;
    #define mem(a) memset(a,0,sizeof(a))
    #define meminf(a) memset(a,127,sizeof(a))
    #define TS printf("111111
    ")
    #define FOR(i,a,b) for( int i=a;i<=b;i++)
    #define FORJ(i,a,b) for(int i=a;i>=b;i--)
    #define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
    #define inf 100000
    inline ll read()
    {
        ll x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    //****************************************
    #define maxn 200
    int n,m,S,visit[maxn][maxn];
    char mp[maxn][50],mps[40][maxn];
    struct ss
    {
        int x,y;
    };
    int main()
    {
    
        int T=read();
        while(T--)
        {
            mem(visit);
            scanf("%d%d",&n,&m);
            FOR(i,1,3)
            {
                scanf("%s",mps[i]+1);
                if(mps[i][1]=='s')
                    S=i;
            }
            if(S==1)S=3;
            else if(S==3)S=1;
            int hh=0,kk=0;
            for(int j=1; j<=n; j++)
            {
                ++hh;
                kk=0;
                for(int i=3; i>=1; i--)
                {
                    mp[hh][++kk]=mps[i][j];
                }
            }
            for(int i=n+1;i<=n+10;i++)
            {
                for(int j=1;j<=3;j++)
                    mp[i][j]='.';
            }
            //cout<<hh<<" "<<kk<<endl;
            bool flag=0;
            queue<ss>Q;
            ss k,lk;
            k.x=1,k.y=S;
            visit[k.x][k.y]=1;
            Q.push(k);
            while(!Q.empty())
            {
                k=Q.front();
                //visit[k.x][k.y]=0;
                Q.pop();
                if(k.x>n)
                {
                    flag=1;
                    break;
                }
                if(mp[k.x+1][k.y]=='.'&&mp[k.x+2][k.y]=='.'&&mp[k.x+3][k.y]=='.')
                {
                    lk.x=k.x+3;
                    lk.y=k.y;
                    if(!visit[lk.x][lk.y])
                        Q.push(lk),visit[lk.x][lk.y]=1;
                }
                if(mp[k.x+1][k.y]=='.'&&mp[k.x+1][k.y+1]=='.'&&mp[k.x+2][k.y+1]=='.'&&mp[k.x+3][k.y+1]=='.'&&k.y+1<=3)
                {
                    lk.x=k.x+3;
                    lk.y=k.y+1;
                    if(!visit[lk.x][lk.y])
                        Q.push(lk),visit[lk.x][lk.y]=1;
                }
                if(mp[k.x+1][k.y]=='.'&&mp[k.x+1][k.y-1]=='.'&&mp[k.x+2][k.y-1]=='.'&&mp[k.x+3][k.y-1]=='.'&&k.y-1>=1)
                {
                    lk.x=k.x+3;
                    lk.y=k.y-1;
                    if(!visit[lk.x][lk.y])
                        Q.push(lk),visit[lk.x][lk.y]=1;
                }
            }
            if(flag)
            {
                cout<<"YES"<<endl;
            }
            else cout<<"NO"<<endl;
        }
        return 0;
    }
    代码
  • 相关阅读:
    P、NP及NPC问题
    latex test3
    latex test2
    test
    整体二分
    bzoj2819 nim (树上带修改查询路径异或和)
    kmp模板题
    KM的三种写法比较
    电视转播
    树状数组处理区间查询和区间修改的问题
  • 原文地址:https://www.cnblogs.com/zxhl/p/4875646.html
Copyright © 2011-2022 走看看