#hihoCoder #1828 : Saving Tang Monk II (分层BFS)

描述

《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts.

During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.

Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace, and he wanted to reach Tang Monk and rescue him.

The palace can be described as a matrix of characters. Different characters stand for different rooms as below:

'S' : The original position of Sun Wukong

'T' : The location of Tang Monk

'.' : An empty room

'#' : A deadly gas room.

'B' : A room with unlimited number of oxygen bottles. Every time Sun Wukong entered a 'B' room from other rooms, he would get an oxygen bottle. But staying there would not get Sun Wukong more oxygen bottles. Sun Wukong could carry at most 5 oxygen bottles at the same time.

'P' : A room with unlimited number of speed-up pills. Every time Sun Wukong entered a 'P' room from other rooms, he would get a speed-up pill. But staying there would not get Sun Wukong more speed-up pills. Sun Wukong could bring unlimited number of speed-up pills with him.

Sun Wukong could move in the palace. For each move, Sun Wukong might go to the adjacent rooms in 4 directions(north, west,south and east). But Sun Wukong couldn't get into a '#' room(deadly gas room) without an oxygen bottle. Entering a '#' room each time would cost Sun Wukong one oxygen bottle.

Each move took Sun Wukong one minute. But if Sun Wukong ate a speed-up pill, he could make next move without spending any time. In other words, each speed-up pill could save Sun Wukong one minute. And if Sun Wukong went into a '#' room, he had to stay there for one extra minute to recover his health.

Since Sun Wukong was an impatient monkey, he wanted to save Tang Monk as soon as possible. Please figure out the minimum time Sun Wukong needed to reach Tang Monk.

输入

There are no more than 25 test cases.

For each case, the first line includes two integers N and M(0 < N,M ≤ 100), meaning that the palace is a N × M matrix.

Then the N×M matrix follows.

The input ends with N = 0 and M = 0.

输出

For each test case, print the minimum time (in minute) Sun Wukong needed to save Tang Monk. If it's impossible for Sun Wukong to complete the mission, print -1

样例输入

2 2
S#
#T
2 5
SB###
##P#T
4 7
SP.....
P#.....
......#
B...##T
0 0

样例输出

-1
8
11

 题目大意 : ’S‘ 为起点, ’T‘ 为终点, ’#‘为有毒气的房间, ’B‘为有氧气罐的房间(可以携带无数个氧气罐,但是进一次’B‘只能带一个), ’P‘ 为加速药水, 可以使下一步不花费时间, ’.' 代表普通道路, 当你进入毒气房间时,必须消耗一瓶氧气罐, 并且从毒气房间出来后, 得休息一个时间单位来恢复健康, 输出从S 到 T的最小时间花费

思路 : 该题的关键在于氧气罐, 氧气罐的数目不同可以将地图分成不同的状态, 想象可以当你携带0个、 1个 、 2个 、 3个 、 4个 、 5个氧气罐时,地图哪些点可以走哪些点不可以走, 就是不同的状态了,理解的话题目也不是很难了

AC代码 :

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e2 + 5;

struct node
{
	int x, y, step, k;  // step代表步数, k代表带了几瓶氧气罐
	bool operator < (const node &oth) const
	{
		return step > oth.step;
	}
};
int n, m, sx, sy;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
char p[MAXN][MAXN];
bool vis[10][MAXN][MAXN];
int bfs() {
	priority_queue <node> q;
	q.push({ sx, sy, 0, 0 });
	while (!q.empty()) {
		node now = q.top();
		q.pop();
		if (vis[now.k][now.x][now.y]) continue;
		vis[now.k][now.x][now.y] = 1;
		for (int i = 0; i < 4; i++) {
			int xx = now.x + dir[i][0];
			int yy = now.y + dir[i][1];
			if (xx < 0 || yy < 0 || xx >= n || yy >= m) continue;
			if (p[xx][yy] == 'T') return now.step + 1;
			else if (p[xx][yy] == '.' || p[xx][yy] == 'S')
				q.push({ xx, yy, now.step + 1, now.k });
			else if (p[xx][yy] == 'B')  // 最多不可以超过5瓶
				q.push({ xx, yy, now.step + 1, min(5, now.k + 1) });
			else if (p[xx][yy] == 'P')
				q.push({ xx, yy, now.step, now.k });
			else if (p[xx][yy] == '#' && now.k)  // 进入毒气房间消耗一瓶氧气罐
				q.push({ xx, yy, now.step + 2, now.k - 1 });
		}
	}
	return -1;
}

int main()
{
	while (cin >> n >> m && n + m) {
		memset(vis, 0, sizeof(vis));  // 注意初始化
		for (int i = 0; i < n; i++) {
			cin >> p[i];
			for (int j = 0; j < m; j++) {
				if (p[i][j] == 'S') sx = i, sy = j;
			}
		}
		int temp = bfs();
		cout << temp << endl;
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值