Saving Tang Monk II HihoCoder-1828 (3D bfs recording)

《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.

Input
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.

Output
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

Sample Input
2 2
S#
#T
2 5
SB###
##P#T
4 7
SP…
P#…
…#
B…##T
0 0
Sample Output
-1
8
11

Question:
Entering from the entrance of S
, it takes one minute to find out how T meets B. You can get an oxygen tank. You can carry up to 5 bottles of oxygen tanks. You will not reap the oxygen tanks after staying. The oxygen tanks can support swk to enter the poisonous gas. room.
If you encounter P, you can get an accelerator, you can carry the accelerator indefinitely, and you can make your one-step action without consuming time.
If you hit #, it means you hit the gas chamber, you need to consume an oxygen cylinder to enter, and it takes two minutes.
If you encounter..., empty the room, it takes a minute.

Problem solution idea:
three-dimensional mark to record the number of oxygen tanks, and then simulate according to the meaning of the problem;

AC code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define debug() puts("what the fuck!")
#define debug(a) cout<<#a<<"="<<a<<endl;
#define speed {
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
#define ll long long
#define mod 998244353
using namespace std;
const double PI = acos(-1.0);
const int maxn = 5e2 + 10;
const int INF = 0x3f3f3f3f;
const double esp_0 = 1e-6;
ll gcd(ll x, ll y) {
    
    
	return y ? gcd(y, x % y) : x;
}
ll lcm(ll x, ll y) {
    
    
	return x * y / gcd(x, y);
}
ll extends_gcd(ll a, ll b, ll& x, ll& y) {
    
    
	if (b == 0) {
    
    
		x = 1;
		y = 0;
		return a;
	}
	ll gcdd = extends_gcd(b, a % b, x, y);
	ll temp = x;
	x = y;
	y = temp - (a / b) * y;
	return gcdd;
}
int n, m;
int vis[maxn][maxn][10];
char mp[maxn][maxn];
int sx, sy;
int dir[4][2] = {
    
     {
    
    1,0},{
    
    -1,0},{
    
    0,1},{
    
    0,-1} };
struct node {
    
    
	int x, y, step, num;
	node(int x, int y, int step, int num) :x(x), y(y), step(step), num(num) {
    
    };
	bool operator <(const node& a)const {
    
    
		return a.step < step;
	}
};
int judge(int x, int y) {
    
    
	if (x >= 0 && x < n && y >= 0 && y < m)return 1;
	return 0;
}
void bfs(int sx, int sy) {
    
    
	priority_queue<node>q;
	q.push(node(sx, sy, 0, 0));
	vis[sx][sy][0] = 1;
	while (!q.empty()) {
    
    
		node temp = q.top();
		q.pop();
		if (mp[temp.x][temp.y] == 'T') {
    
    
			cout << temp.step << endl;
			return;
		}
		for (int i = 0; i < 4; ++i) {
    
    
			int nextx = temp.x + dir[i][0];
			int nexty = temp.y + dir[i][1];
			if (judge(nextx, nexty)) {
    
    
				if ((mp[nextx][nexty] == '.' || mp[nextx][nexty] == 'S' || mp[nextx][nexty] == 'T')&&!vis[nextx][nexty][temp.num]) {
    
    
					vis[nextx][nexty][temp.num] = 1;
					q.push(node(nextx, nexty, temp.step + 1, temp.num));
				}
				else if (mp[nextx][nexty] == '#' && temp.num > 0 && !vis[nextx][nexty][temp.num - 1]) {
    
    
					vis[nextx][nexty][temp.num - 1] = 1;
					q.push(node(nextx, nexty, temp.step + 2, temp.num - 1));
				}
				else if (mp[nextx][nexty] == 'B' && !vis[nextx][nexty][temp.num + 1] && temp.num + 1 <= 5) {
    
    
					vis[nextx][nexty][temp.num + 1] = 1;
					q.push(node(nextx, nexty, temp.step + 1, temp.num + 1));
				}
				else if (mp[nextx][nexty] == 'B' && !vis[nextx][nexty][temp.num] && temp.num >= 5) {
    
    
					vis[nextx][nexty][temp.num] = 1;
					q.push(node(nextx, nexty, temp.step + 1, temp.num));
				}
				else if (mp[nextx][nexty] == 'P' && !vis[nextx][nexty][temp.num]) {
    
    
					vis[nextx][nexty][temp.num] = 1;
					q.push(node(nextx, nexty, temp.step, temp.num));
				}
			}
		}
	}
	cout << "-1" << endl;
}
int main() {
    
    
	while (~scanf("%d%d", &n, &m), n, m) {
    
    
		for (int i = 0; i < n; ++i) {
    
    
			scanf("%s", mp[i]);
			for (int j = 0; j < m; ++j) {
    
    
				if (mp[i][j] == 'S') {
    
    
					sx = i;
					sy = j;
				}
			}
		}
		mem(vis, 0);
		bfs(sx, sy);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_40924271/article/details/109912259