Description:

《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

题目链接

bfs搜索完所有情况,用一个三维数组记录到达每个点时携带不同氧气瓶数量分别的最小步数(到达一个点状态差别只和携带氧气瓶数相关),在bfs时若搜到到达一点携带相同氧气瓶数但步数更多时不加入队列已达到去重效果(感觉这里可以hash去重,没有试),最后在重点所有氧气瓶数中找到最小步数即为最终结果。

AC代码:

#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 5;

struct Statu {
	int X, Y, Step, Oxygen, Pill;
};

int N, M;
int StartX, StartY;
int EndX, EndY;
char Maze[maxn][maxn];
int Step[maxn][maxn][10];

void Bfs() {
	queue<Statu> Que;
	Que.push(Statu {StartX, StartY, 0, 0, 0});
	while (!Que.empty()) {
		Statu Keep = Que.front();
		Que.pop();
		for (int i = -1; i <= 1; ++i) {
			for (int j = -1; j <= 1; ++j) {
				if (abs(i) != abs(j)) {
					int NX = Keep.X + i, NY = Keep.Y + j;
					if (NX >= 0 && NX < N && NY >= 0 && NY < M) {
						if (Maze[NX][NY] == '#') {
							if (Keep.Oxygen > 0) {
								if (Keep.Step + 2 - Keep.Pill < Step[NX][NY][Keep.Oxygen - 1]) {
									Que.push(Statu {NX, NY, Keep.Step + 2, Keep.Oxygen - 1, Keep.Pill});
									Step[NX][NY][Keep.Oxygen - 1] = Keep.Step + 2 - Keep.Pill;
								}
							}
						}
						else if (Maze[NX][NY] == 'B') {
							if (Keep.Oxygen == 5) {
								if (Keep.Step + 1 - Keep.Pill < Step[NX][NY][Keep.Oxygen]) {
									Que.push(Statu {NX, NY, Keep.Step + 1, Keep.Oxygen, Keep.Pill});
									Step[NX][NY][Keep.Oxygen] = Keep.Step + 1 - Keep.Pill;
								}
							}
							else {
								if (Keep.Step + 1 - Keep.Pill < Step[NX][NY][Keep.Oxygen + 1]) {
									Que.push(Statu {NX, NY, Keep.Step + 1, Keep.Oxygen + 1, Keep.Pill});
									Step[NX][NY][Keep.Oxygen + 1] = Keep.Step + 1 + Keep.Pill;
								}
							}
						}
						else if (Maze[NX][NY] == 'P') {
							if (Keep.Step - Keep.Pill < Step[NX][NY][Keep.Oxygen]) {
								Que.push(Statu {NX, NY, Keep.Step + 1, Keep.Oxygen, Keep.Pill + 1});
								Step[NX][NY][Keep.Oxygen] = Keep.Step - Keep.Pill;
							}
						}
						else {
							if (Keep.Step + 1 - Keep.Pill < Step[NX][NY][Keep.Oxygen]) {
								Que.push(Statu {NX, NY, Keep.Step + 1, Keep.Oxygen, Keep.Pill});
								Step[NX][NY][Keep.Oxygen] = Keep.Step + 1 - Keep.Pill;
							}
						}
					}
				}
			}
		}
	}
}

int main(int argc, char *argv[]) {
	while (~scanf("%d%d", &N, &M) && N + M) {
		for (int i = 0; i < N; ++i) {
			scanf("%s", Maze[i]);
			for (int j = 0; j < M; ++j) {
				for (int k = 0; k < 6; ++k) {
					Step[i][j][k] = INF;
				}
				if (Maze[i][j] == 'S') {
					StartX = i; StartY = j;
				}
				else if (Maze[i][j] == 'T') {
					EndX = i; EndY = j;
				}
			}
		}
		Bfs();
		int Ans = INF;
		for (int i = 0; i < 6; ++i) {
			Ans = min(Ans, Step[EndX][EndY][i]);
		}
		Ans = Ans == INF ? -1 : Ans;
		printf("%d\n", Ans);
	}
	return 0;
}