hdu 1072 Nightmare

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1072

一个人梦见自己背着炸弹走迷宫,炸弹会在6min后爆炸。若能从起点走到终点输出最短时间,否则输出-1。

矩阵数字0:不可走 1:可走2:起点3:终点4:炸弹时间重置为6

广搜就可以,用一个结构体成员time记录剩余爆炸时间,step记录步数。

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int dir[][2] = {0,-1, -1,0, 0,1, 1,0};
int n, m, map[10][10], startx, starty;
struct Node
{
	int x, y, time, step;
};
int bfs()
{
	Node now, next;
	now.x = startx, now.y = starty, now.time = 6, now.step = 0;
	map[now.x][now.y] = 0;
	queue<Node> q;
	q.push(now);
	while (!q.empty())
	{
		now = q.front();
		q.pop();
		for (int i = 0; i < 4; ++i)
		{
			next.x = now.x + dir[i][0], next.y = now.y + dir[i][1];
			next.time = now.time - 1, next.step = now.step + 1;
			if (next.x>=0 && next.x<n && next.y>=0 && next.y<m &&
					map[next.x][next.y] != 0 && next.time > 0)
			{
				if (map[next.x][next.y] == 3)
					return next.step;
				else if (map[next.x][next.y] == 4)
				{
					next.time = 6;
					map[next.x][next.y] = 0;
				}
				q.push(next);
			}		
		}
	}
	return -1;
}
int main()
{
 	int T;
 	scanf("%d", &T);
 	while (T--)
 	{
	    scanf("%d %d", &n, &m);
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < m; ++j)
			{
				scanf("%d", &map[i][j]);
				if (map[i][j] == 2)
				{
					startx = i;
					starty = j;
				}
			}
		printf("%d\n", bfs());
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PegasusWang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值