Codeforces Round #325 (Div. 2) D. Phillip and Trains BFS

Source: Internet
Author: User

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 are in some cell of the leftmost column. Some number of trains rides towards the hero. Each train consists of both or more neighbouring cells in some row of the field.

All trains is moving from right to left at a speed of both cells per second, and the hero runs from Peed of one cell per second. For simplicity, the game was implemented so, 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 was 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 are a sequence of movements of Philip, such that he would being 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 sections 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 the 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 whe Re the game is on. Philip's initial positionis marked as ' s ', he's in the leftmost column. Each of the K trains are marked by some sequence of identical uppercase letters of the Chinese alphabet, loc Ated in one line. DISTINCT trains is represented by Distinct letters. Character '. ' Represents an empty cell, that's, 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 was possible to win the game and word NO othe Rwise.

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

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 for forward, then go forward and climb to the second row, go forward again and go on to the first row. After that train blocks Philip's path, so he can go straight to the end of the tunnel.

Note the This problem the challenges is restricted to tests that contain only one testset.

Test Instructions: give you a 3*n figure, the beginning of people in the leftmost first column of a position, each person must go to the right one grid, and then down or up, do not move a grid, people walk, all trains from right to left two, ask you whether you can go to the right side of this column

The puzzle: it is the meaning of the people to move to the right and then the two-grid, BFS is good

///1085422276#include <bits/stdc++.h>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#defineMeminf (a) memset (A,127,sizeof (a))#defineTS printf ("111111\n")#definefor (I,A,B) for (int i=a;i<=b;i++)#defineForj (I,A,B) for (int i=a;i>=b;i--)#defineREAD (a,b,c) scanf ("%d%d%d", &a,&b,&c)#defineINF 100000inline ll read () {ll x=0, f=1; CharCh=GetChar ();  while(ch<'0'|| Ch>'9')    {        if(ch=='-') f=-1; CH=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); }    returnx*F;}//****************************************#defineMAXN 200intN,M,S,VISIT[MAXN][MAXN];Charmp[maxn][ -],mps[ +][MAXN];structss{intx, y;};intMain () {intt=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; intHh=0, kk=0;  for(intj=1; j<=n; J + +)        {            ++hh; KK=0;  for(intI=3; i>=1; i--) {mp[hh][++kk]=Mps[i][j]; }        }         for(inti=n+1; i<=n+Ten; i++)        {             for(intj=1; j<=3; j + +) Mp[i][j]='.'; }        //cout<        BOOLflag=0; Queue<ss>p;        SS K,lk; k.x=1, k.y=R; 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; }        Elsecout<<"NO"<<Endl; }    return 0;}
Code

Codeforces Round #325 (Div. 2) D. Phillip and Trains BFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.