描述

《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
 /*
堆优化的广搜 需要注意的是使用标记数组记录的是哪些点已经走过了,这里使用三维的标记数组标记哪些点的第几次走过,不再
对其进行拓展。
*/
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; const int maxn = + ;
int n, m;
int sx, sy, ex, ey;
char e[maxn][maxn];
int nex[][] = {,, ,, ,-, -,};
bool bk[maxn][maxn][]; struct Node {
int x, y, B, P, s;
bool operator < (const Node &a) const {
if(a.s == s)
return a.B > B;
return a.s < s;
}
}; int BFS() {
memset(bk, , sizeof(bk));
priority_queue<Node> q;
bk[sx][sy][] = ;
q.push((Node){sx, sy, , , }); int T = ;
while(!q.empty()) {
Node tmp = q.top();
q.pop(); if(e[tmp.x][tmp.y] == 'T')
return tmp.s; for(int i = ; i < ; i++) {
int tx = tmp.x + nex[i][];
int ty = tmp.y + nex[i][]; if(tx < || tx > n || ty < || ty > m || bk[tx][ty][tmp.B])
continue;
bk[tx][ty][tmp.B] = ; if(e[tx][ty] == 'T')
return tmp.s + ;
else if(e[tx][ty] == '#') {
if(tmp.B > ) {
q.push((Node){tx, ty, tmp.B - , tmp.P, tmp.s + });
}
}
else if(e[tx][ty] == 'B') {
if(tmp.B < )
q.push((Node){tx, ty, tmp.B + , tmp.P, tmp.s + });
else
q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s + });
}
else if(e[tx][ty] == 'P') {
q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s});
}
else if(e[tx][ty] == '.') {
q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s + });
}
else if(e[tx][ty] == 'S') {
q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s + });
}
}
}
return -;
} int main()
{
while(scanf("%d%d", &n, &m) == && n + m != ) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
scanf(" %c", &e[i][j]);
if(e[i][j] == 'S') {
sx = i;
sy = j;
}
}
} printf("%d\n", BFS());
}
return ;
}

hihocoder #1828 : Saving Tang Monk II(BFS)的更多相关文章

  1. 北京2018网络赛 hihocoder#1828 : Saving Tang Monk II (BFS + DP +多开一维)

    hihocoder 1828 :https://hihocoder.com/problemset/problem/1828 学习参考:https://www.cnblogs.com/tobyw/p/9 ...

  2. hihocoder 1828 Saving Tang Monk II (DP+BFS)

    题目链接 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great C ...

  3. ACM-ICPC2018北京网络赛 Saving Tang Monk II(bfs+优先队列)

    题目1 : Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also < ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II(优先队列广搜)

    #include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN]; ]; int n, m, sx, sy, ex, ...

  5. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】

    任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...

  6. Saving Tang Monk II(bfs+优先队列)

    Saving Tang Monk II https://hihocoder.com/problemset/problem/1828 时间限制:1000ms 单点时限:1000ms 内存限制:256MB ...

  7. Saving Tang Monk II HihoCoder - 1828 2018北京赛站网络赛A题

    <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chines ...

  8. hihoCoder-1828 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II BFS

    题面 题意:N*M的网格图里,有起点S,终点T,然后有'.'表示一般房间,'#'表示毒气房间,进入毒气房间要消耗一个氧气瓶,而且要多停留一分钟,'B'表示放氧气瓶的房间,每次进入可以获得一个氧气瓶,最 ...

  9. Saving Tang Monk II

    题目链接:http://hihocoder.com/contest/acmicpc2018beijingonline/problem/1 AC代码: #include<bits/stdc++.h ...

随机推荐

  1. jdk8 永久代变更

    java8 去掉了永久代permgen(又称非堆,其实也是堆的一部分),类的方法代码,常亮,方法名,静态变量等存放在永久代中 改为使用元空间 Metaspace , Metaspace 不在是堆的一部 ...

  2. 【机器学习】Octave 实现逻辑回归 Logistic Regression

    ex2data1.txt ex2data2.txt 本次算法的背景是,假如你是一个大学的管理者,你需要根据学生之前的成绩(两门科目)来预测该学生是否能进入该大学. 根据题意,我们不难分辨出这是一种二分 ...

  3. 向Word添加一段文本

    文档层次结构 [段落之后] 是一段连续文本,它定义具有一组常见属性的文本区域.一段连续文本由 r 元素表示,这样创建器便可组合换行.样式或格式设置属性,从而将相同信息应用于一段连续文本的所有部分. 正 ...

  4. ReSharper 10.0.0.2 Ultimate 破解

    文件下载地址:http://pan.baidu.com/s/1gf7l8cF 1.安装ReSharper 10.0.0.2 Ultimate 2.修改Products.json文件的FilePath, ...

  5. HPS基本概念及其设计

    DE1-SOC开发版上的FPGA在一个基于ARM的用户定制系统(SOC)中集成了分立处理器(HPS).FPGA和数字信号处理(DSP)功能.HPS是基于ARM cortex-A9双核处理器,具有丰富的 ...

  6. PMP:7.项目成本管理

    内容中包含 base64string 图片造成字符过多,拒绝显示

  7. Windows核心编程:第10章 同步设备IO与异步设备IO

    Github https://github.com/gongluck/Windows-Core-Program.git //第10章 同步设备IO与异步设备IO.cpp: 定义应用程序的入口点. // ...

  8. 项目Alpha冲刺(团队3/10)

    项目Alpha冲刺(团队3/10) 团队名称: 云打印 作业要求: 项目Alpha冲刺(团队) 作业目标: 完成项目Alpha版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 ...

  9. HTML中META标签的使用

    一.META标签简介 <meta> 元素可提供有关页面的元信息,元数据总是以名称/值的形式被成对传递的. <meta> 标签位于文档的头部,不包含任何内容. <meta& ...

  10. centos7.2 安装 mysql5.7

    一.MySQL 5.7 主要特性: 原生支持 Systemd 更好的性能:对于多核 CPU.固态硬盘.锁有着更好的优化更好的 InnoDB 存储引擎 更为健壮的复制功能:复制带来了数据完全不丢失的方案 ...