Nightmare bfs

Nightmare
原题链接https://vjudge.net/contest/345248#problem/J
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
题意为身上有一个炸弹,炸弹会在6步时爆炸,我们需要在6步前到达3的位置才能逃离,图中还会有4,4会将炸弹的时间清零,也就是说从6开始重新计数。由于可能需要绕到4的位置去重置时间再次前进,所以本题不需要标记已走过的路程,但是要注意改变已走过的4的值,防止陷入死循环。

#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
struct node
{
	long long x;
	long long y;
	long long step;
	long long time;
} t1,t2;
long long n,m;
long long map[105][105];
long long mv[4][2]= {1,0,-1,0,0,1,0,-1};
long long bfs()
{
	queue<node>q;
	q.push(t1);
	node s,e;
	vis[t1.x][t1.y]=1;
	long long i,j;
	while(!q.empty())
	{
		s=q.front();
	//	printf("%lld %lld***\n",s.x,s.y);
		q.pop();
		if(s.x==t2.x&&s.y==t2.y)
		{
			return s.step;
		}
		for(i=0; i<4; i++)
		{
			e.x=s.x+mv[i][0];
			e.y=s.y+mv[i][1];
		//	printf("***%lld %lld\n",e.x,e.y);
			e.time=s.time-1;
			if(e.time==0)
			{
				continue;
			}
			if(e.x<0||e.y<0||e.x>n-1||e.y>m-1||map[e.x][e.y]==0)
			{
				continue;
			}
			if(map[e.x][e.y]==4)//走过一次之后变成正常的路
			{
				e.time=6;
				map[e.x][e.y]=1;
			}
			e.step=s.step+1;
		//	printf("********\n");
		//	printf("%lld %lld\n",e.x,e.y);
			q.push(e);
		}
	}
	return -1;
}
int main()
{
	long long t;
	scanf("%lld",&t);
	while(t--)
	{
		memset(map,0,sizeof(map));
		scanf("%lld %lld",&n,&m);
		long long i,j;
		for(i=0; i<n; i++)
		{
			for(j=0; j<m; j++)
			{
				scanf("%lld",&map[i][j]);
				if(map[i][j]==2)
				{
					t1.x=i;
					t1.y=j;
					t1.step=0;
					t1.time=6;
				}
				else if(map[i][j]==3)
				{
					t2.x=i;
					t2.y=j;
				}
			}
		}
		long long sum=bfs();
		printf("%lld\n",sum);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值