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

来源:互联网 发布:lol网络不稳 编辑:程序博客网 时间:2024/05/08 22:13

原题链接
D. Phillip and Trains
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
题意&思路:这个题有点类似于前一段时间手机上一个比较有名的游戏地铁跑酷,他的移动规则是人先向右移动一格,然后可以瞬间移动到上一格或者是下一格(如果可以),然后车会向左走两格,其实根据相对论我们可以认为车没有动而人又向右走了两步。结束的标志就是人撞上了火车或者是到达了最右边。这个问题可以用DFS+记忆化搜索来做也可以用BFS来做,我是用BFS加记忆化,将确定不可能到达右边的情况索性也认为是车。同时使用X坐标大的优先级高的优先队列来优化这个BFS。如果不加记忆化就会超时。
以下是部分测试数据

816 4...AAAAA........s.BBB......CCCCC........DDDDD...16 4...AAAAA........s.BBB....CCCCC.........DDDDD....10 4s.ZZ...........AAABB.YYYYYY...10 4s.ZZ..........AAAABB.YYYYYY...100 1AA..................................................................................................s...................................................A.............................................AA..................................................................................................AA97 23......EEE.PPPPBBBBB.FFF..DDDDD.......VVV.......SSS......KKKK......QQQQQQ.TTTTTT......JJJ.........s.........................HHH..........OO.....NNNNNN...CCCC..............UUUU............MMMMGGG............AAAAA....LLL........................................II..................RRR...WWWWW...100 20.................XXXXXLL.ZZZ.............CCC..................PPPP..............SSSSS...IIIII.......s.....WWWWWWW.................VV......KK..............RR........................................JJJJ....QQQ...........TTTTTTT.BB...............................EEEEEEE....FFFF.DDD...YYYY......NNNNN....100 5............................................AAAAAA..................................................s...................................................CCC...BBBBBBDDDDDD................................................................................................EEEEEE............................
//http://codeforces.com/problemset/problem/586/D#include <algorithm>#include <iostream>#include <utility>#include <sstream>#include <cstring>#include <cstdio>#include <vector>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>using namespace std;typedef long long ll;const int MOD = int(1e9) + 7;//int MOD = 99990001;const int INF = 0x3f3f3f3f;const ll INFF = (~(0ULL)>>1);const double EPS = 1e-9;const double OO = 1e20;const double PI = acos(-1.0); //M_PI;const int fx[] = {0,-1,1};const int maxn=100 + 300;int a[3][maxn];int ok=0;//代表当前还没有找到int n,k;struct node{    int i,j;    friend bool operator < (node x,node y){        return x.j < y.j;//这样j小的在队列底    }};bool can(int x,int y){        if(a[x][y]==1 && a[x][y+1]==1 && a[x][y+2]==1) return true;        else return false;}void bfs(int loci,int locj){        priority_queue<node> q;        q.push((node){loci,locj});        while(!q.empty()){                node now=q.top();                q.pop();//取队列的首位,并删除首位                //cout << now.i << " " << now.j << endl;                if(a[now.i][now.j+1]==0) continue;                bool flag=false;                for(int i=0;i<3;i++){                        int newi=now.i+fx[i];                        int newj=now.j+1;                        if(can(newi,newj)){                                if(newj+2>=n){                                        ok=1;                                        return;                                }                                q.push((node){newi,newj+2});//新状态入队列                                flag=true;                        }                }                a[now.i][now.j]=0;        }}int main(){        int T;        scanf("%d",&T);        while(T--){            memset(a,0,sizeof(a));//0 no   1 can            scanf("%d%d",&n,&k);            int loci=0,locj=0;            for(int i=0;i<3;i++){                char t;                for(int j=0;j<n;j++){                    scanf(" %c",&t);                    if(t=='.'){                        a[i][j]=1;                    }                    else if(t=='s'){                        a[i][j]=1;                        loci=i;                        locj=j;                    }                }                for(int j=n;j<105;j++)                    a[i][j]=1;            }            ok=0;            bfs(loci,locj);            if(ok==1) cout << "YES" << endl;            else cout << "NO" << endl;        }        return 0;}
0 0
原创粉丝点击