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

题解

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

按照题意 分层BFS搜索即可

AC代码

#include 
#include 
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 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;
}

你可能感兴趣的:(ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A. Saving Tang Monk II)