Phillip and Trains bfs 思维

游戏“Subway Roller”的通关搜索问题
博客介绍了移动应用商店的新游戏“Subway Roller”,游戏主角Philip在隧道中,隧道是三行n列矩形区域,有火车驶向主角。题目可转化为superman移动问题,先走一步再枚举方向走二步,然后进行搜索。

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.

题目大意:一个superman,以肉身合道  (刚火车) superman 先走,ta可以向前走一步再调整位置,然后火车向他的方向走二步,看看ta是否能到达边缘。

嗯,转化一下,都是superman在走,所以ta先走一步再枚举方向再走二步。然后就可以搜索了

#include<iostream>
#include<queue>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cctype>
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int> pi;
char gap[5][150];
int visit[5][150];
int n,k;
pi s;


bool bfs()
{
	queue<pi> q;
	q.push(s);
	while(!q.empty())
	{
		int x=q.front().first,y=q.front().second;
		q.pop();
		if(y>=n)
		{
			return 1;
		}
		if(isalpha(gap[x][++y])) continue;
		for(int i=-1;i<=1;i++)
		{
			//cout<<"x";
			int tempx=x+i;
			if(tempx<1||tempx>3) continue;
			if(isalpha(gap[tempx][y])||isalpha(gap[tempx][y+1])||isalpha(gap[tempx][y+2])||visit[tempx][y+2]==1)continue;
			visit[tempx][y+2]=1;
			if(y+2>=n) return 1;
			q.push(pi(tempx,y+2));
		}
	}
	return 0;
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(visit,0,sizeof(visit));
		scanf("%d%d",&n,&k);
		for(int i=1;i<=3;i++)
		{
			scanf("%s",gap[i]+1);			
			if(gap[i][1]=='s') s.first=i,s.second=1;
		}
		visit[s.first][s.second]=1;
		puts(bfs() ? "YES" : "NO");
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值