Codeforces 586D Phillip and Trains

来源:互联网 发布:免费取女装淘宝店名 编辑:程序博客网 时间:2024/05/20 07:58
D. Phillip and Trainstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe 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.InputEach 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.OutputFor 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.ExamplesInput216 4...AAAAA........s.BBB......CCCCC........DDDDD...16 4...AAAAA........s.BBB....CCCCC.........DDDDD....OutputYESNOInput210 4s.ZZ...........AAABB.YYYYYY...10 4s.ZZ..........AAAABB.YYYYYY...OutputYESNONoteIn 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.

题意:
菲利普每秒向右移动一格 然后可以选择向上/向下/不动
然后火车每秒向左移动2格 问菲利普最后能不能到达最右边


bfs即可 至于火车的移动 可以看作人向右移动2格(相对位移)
注意避免重复添加一些点到bfs中 可能导致mle

#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>const int N=100+5;char mp[3][N];const int dir[2][2]={    {-1,0},{1,0}};bool isTrain(char x){    return x>='A'&&x<='Z';}bool slove(int n){    deque<pii>de;    for(int i=0;i<3;++i)        if(mp[i][0]=='s')            de.push_back({i,0});    while(!de.empty()){        int i=de[0].first,j=de[0].second;        de.pop_front();        mp[i][j]='.';        ++j;        if(j<n&&isTrain(mp[i][j]))            continue;        if(j>=n-1)            return true;        if(!isTrain(mp[i][j+1])&&!isTrain(mp[i][j+2])&&mp[i][j+2]!='s'){            mp[i][j+2]='s';            de.push_back({i,j+2});        }        for(int k=0;k<2;++k){            int ni=i+dir[k][0],nj=j+dir[k][1];            if(ni>=0&&ni<3&&                    !isTrain(mp[ni][nj])&&                    !isTrain(mp[ni][nj+1])&&                    !isTrain(mp[ni][nj+2])){                if(mp[ni][nj+2]!='s'){                    mp[ni][nj+2]='s';                    de.push_back({ni,nj+2});                }                if(nj>=n-1)                    return true;            }        }    }    return false;}int main(){    //freopen("/home/lu/文档/r.txt","r",stdin);    //freopen("/home/lu/文档/w.txt","w",stdout);    int T,n,k;    scanf("%d",&T);    while(T--){        scanf("%d%d",&n,&k);        for(int i=0;i<3;++i)            scanf("%s",mp[i]);        puts(slove(n)?"YES":"NO");    }    return 0;}
0 0