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

水题,bfs一下即可。。。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

#define mp(x, y) make_pair(x, y)
const int maxn = 105;

queue<pair<int, int> >q;
int vis[5][maxn];
char g[5][maxn];
int n, m;

bool check(int x, int y)
{
	if(x <= 0 || x >= 4 || y <= 0 || y > n) return false;
	if(g[x][y] != '.') return false;
	return true;
}

void bfs()
{
	for(int i = 1; i <= 3; i++)
		for(int j = 1; j <= n; j++)
			if(g[i][j] == 's') {
				q.push(mp(i, j));
			}
	while(!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if(vis[x][y] == 1) continue;
		vis[x][y] = 1;
		
		if(check(x, y+1)) {
			vis[x][y+1] = 2;
			if(check(x, y+2)) {
				vis[x][y+2] = 2;
			}
			if(check(x-1, y+1)) {
				vis[x-1][y+1] = 2;
				if(check(x-1, y+2)) vis[x-1][y+2] = 2;
			}
			if(check(x+1, y+1)) {
				vis[x+1][y+1] = 2;
				if(check(x+1, y+2)) vis[x+1][y+2] = 2;
			}
		}
		
		if(check(x, y+1) && check(x-1, y+1) && check(x-1, y+2) && check(x-1, y+3)) q.push(mp(x-1, y+3));
		if(check(x, y+1) && check(x, y+2) && check(x, y+3)) q.push(mp(x, y+3));
		if(check(x, y+1) && check(x+1, y+1) && check(x+1, y+2) && check(x+1, y+3)) q.push(mp(x+1, y+3));
	}
}

void debug()
{
	for(int i = 1; i <= 3; i++) {
		for(int j = 1; j <= n; j++) {
			printf("%d ", vis[i][j]);
		}
		printf("\n");
	}
}

void work()
{
	scanf("%d%d", &n, &m);
	memset(vis, 0, sizeof vis);
	for(int i = 1; i <= 3; i++) {
		scanf("%s", g[i]+1);
	}
	bfs();
	
	for(int i = 1; i <= 3; i++) if(vis[i][n]) {
		printf("YES\n");
		return;
	}
	
	printf("NO\n");
}

int main()
{
//freopen("data", "r", stdin);
	int _;
	scanf("%d", &_);
	while(_--) work();
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值