ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A. Saving Tang Monk II

题解

题目大意 给一个图S是起点 T是终点 .是空房间 #是毒气室 B是氧气瓶存放室 P是加速室
每次走到空房间或者起点消耗1秒 走到氧气室获得一个氧气瓶最多携带5个氧气瓶 进入毒气室需要一瓶氧气并且消耗2秒 进入加速室不消耗时间(可以这么理解) 问S走到T的最短时间

按照题意 分层BFS搜索即可

AC代码

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

const int INF = 0x3f3f3f3f;
const int MAXN = 110;
int n, m;
int dir[4][2] = { -1, 0, 1, 0, 0, -1, 0, 1 };
char g[MAXN][MAXN];
int vis[10][MAXN][MAXN];

struct node
{
	int y, x, c, k;
	bool operator < (const node &oth) const
	{
		return this->c > oth.c;
	}
};
int BFS(int y, int x)
{
	priority_queue<node> pq;
	pq.push({ y, x, 0, 0 });
	while (!pq.empty())
	{
		y = pq.top().y, x = pq.top().x;
		int c = pq.top().c, k = pq.top().k;
		pq.pop();
		if (vis[k][y][x])
			continue;
		vis[k][y][x] = 1;
		for (int i = 0; i < 4; i++)
		{
			int xx = x + dir[i][1], yy = y + dir[i][0];
			if (yy < 1 || yy > n || xx < 1 || xx > m)
				continue;
			if (g[yy][xx] == 'T')
				return c + 1;
			else if (g[yy][xx] == '.' || g[yy][xx] == 'S')
				pq.push({yy, xx, c + 1, k});
			else if (g[yy][xx] == '#' && k)
				pq.push({ yy, xx, c + 2, k - 1});
			else if (g[yy][xx] == 'B')
				pq.push({ yy, xx, c + 1, min(5, k + 1) });
			else if (g[yy][xx] == 'P')
				pq.push({ yy, xx, c, k });
		}
	}
	return -1;
}
int main()
{
#ifdef LOCAL
	freopen("C:/input.txt", "r", stdin);
#endif
	while (cin >> n >> m, n && m)
	{
		memset(vis, 0, sizeof(vis));
		memset(g, 0, sizeof(g));
		int x, y;
		for (int i = 1; i <= n; i++)
		{
			scanf("%s", g[i] + 1);
			for (int j = 1; j <= m; j++)
				if (g[i][j] == 'S')
					x = j, y = i;
		}
		int res = BFS(y, x);
		printf("%d\n", res);
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值