[CodeForces586D]Phillip and Trains

Description

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.
image.png-42.6kB

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 Input

2
16 4
...AAAAA........
s.BBB......CCCCC
........DDDDD...
16 4
...AAAAA........
s.BBB....CCCCC..
.......DDDDD....


2
10 4
s.ZZ......
.....AAABB
.YYYYYY...
10 4
s.ZZ......
....AAAABB
.YYYYYY...
Sample Output

YES
NO


YES
NO
题解

预处理一下不能走的位置,dp

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
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;
}
char a[5][105];
int t,n,k,cnt,hsh[256],len[30],X[30],Y[30],ban[105][5][105],dp[5][105];
int main()
{
	t=read();
	while(t--)
	{
		memset(hsh,0,sizeof(hsh));
		memset(dp,0,sizeof(dp));
		memset(ban,0,sizeof(ban));
		cnt=0;
		int pos;
		n=read(),k=read();
		for(int i=1;i<=3;i++)gets(a[i]+1);
		for(int i=1;i<=3;i++)if(a[i][1]=='s')dp[i][1]=1,pos=i;
		for(int i=1;i<=3;i++)for(int j=1;j<=n;j++)if(a[i][j]!='s'&&a[i][j]!='.')
		{
			if(hsh[a[i][j]])len[hsh[a[i][j]]]++;
			else hsh[a[i][j]]=++cnt,X[cnt]=i,Y[cnt]=j,len[cnt]=1;
		}
		bool ok=0;
		for(int i=1;i<n;i++)for(int j=1;j<=k;j++)
		{
			for(int h=Y[j]-3;h<Y[j]+len[j];h++)if(h>0)ban[i][X[j]][h]=1;
			if(i==1&&X[j]==pos&&Y[j]==2)ok=1;
			Y[j]-=2;
		}
		if(ok)
		{
			puts("NO");
			continue;
		}
		for(int i=1;i<n;i++)for(int j=1;j<=3;j++)if(dp[j][i])for(int h=-1;h<=1;h++)
		{
			int tx=j+h,ty=i+1;if(tx==0||tx==4)continue;
			if(ban[i][tx][ty])continue;
			dp[tx][ty]=1;
		}
		bool flag=0;
		for(int i=1;i<=3;i++)if(dp[i][n])flag=1;
		if(flag)puts("YES");
		else puts("NO");
	}
	return 0;
}
posted @ 2018-04-11 22:18  LJZ_C  阅读(278)  评论(0编辑  收藏  举报