描述

《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
 1 /*
 2 堆优化的广搜
 3
 4 需要注意的是使用标记数组记录的是哪些点已经走过了,这里使用三维的标记数组标记哪些点的第几次走过,不再
 5 对其进行拓展。
 6 */
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <queue>
10 using namespace std;
11
12 const int maxn = 100 + 10;
13 int n, m;
14 int sx, sy, ex, ey;
15 char e[maxn][maxn];
16 int nex[4][2] = {0,1, 1,0, 0,-1, -1,0};
17 bool bk[maxn][maxn][10];
18
19 struct Node {
20     int x, y, B, P, s;
21     bool operator < (const Node &a) const {
22         if(a.s == s)
23             return a.B > B;
24         return a.s < s;
25     }
26 };
27
28 int BFS() {
29     memset(bk, 0, sizeof(bk));
30     priority_queue<Node> q;
31     bk[sx][sy][0] = 1;
32     q.push((Node){sx, sy, 0, 0, 0});
33
34     int T = 0;
35     while(!q.empty()) {
36         Node tmp = q.top();
37         q.pop();
38
39         if(e[tmp.x][tmp.y] == 'T')
40             return tmp.s;
41
42         for(int i = 0; i < 4; i++) {
43             int tx = tmp.x + nex[i][0];
44             int ty = tmp.y + nex[i][1];
45
46             if(tx < 1 || tx > n || ty < 1 || ty > m || bk[tx][ty][tmp.B])
47                 continue;
48             bk[tx][ty][tmp.B] = 1;
49
50             if(e[tx][ty] == 'T')
51                 return tmp.s + 1;
52             else if(e[tx][ty] == '#') {
53                 if(tmp.B > 0) {
54                     q.push((Node){tx, ty, tmp.B - 1, tmp.P, tmp.s + 2});
55                 }
56             }
57             else if(e[tx][ty] == 'B') {
58                 if(tmp.B < 5)
59                     q.push((Node){tx, ty, tmp.B + 1, tmp.P, tmp.s + 1});
60                 else
61                     q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s + 1});
62             }
63             else if(e[tx][ty] == 'P') {
64                 q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s});
65             }
66             else if(e[tx][ty] == '.') {
67                 q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s + 1});
68             }
69             else if(e[tx][ty] == 'S') {
70                 q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s + 1});
71             }
72         }
73     }
74     return -1;
75 }
76
77 int main()
78 {
79     while(scanf("%d%d", &n, &m) == 2 && n + m != 0) {
80         for(int i = 1; i <= n; i++) {
81             for(int j = 1; j <= m; j++) {
82                 scanf(" %c", &e[i][j]);
83                 if(e[i][j] == 'S') {
84                     sx = i;
85                     sy = j;
86                 }
87             }
88         }
89
90         printf("%d\n", BFS());
91     }
92     return 0;
93 }

转载于:https://www.cnblogs.com/wenzhixin/p/9695988.html

hihocoder #1828 : Saving Tang Monk II(BFS)相关推荐

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

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

  2. HihoCoder - 1828 Saving Tang Monk II(bfs+动态规划/bfs+优先队列)

    题目链接:点击查看 题目大意:孙悟空要走迷宫去救唐僧,给出n和m约束迷宫大小: 迷宫中: S代表起点 T代表终点 B代表氧气区,经过可以获得一罐氧气,最多储存5罐氧气 #代表毒气区,经过需要花费2个时 ...

  3. Saving Tang Monk II HihoCoder - 1828(2018北京网络赛三维标记+bfs)

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

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A Saving Tang Monk II【分层bfs】

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also <Monkey>) is one of the ...

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

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

  6. 2018年 ICPC北京网络预选赛 A题 Saving Tang Monk II

    由于个人比较菜啊,这道题错了4次才ac,思路是对的,就是各种死在细节上. 题目大意:唐僧给妖精抓走了,悟空要去救出唐僧,给你一个地图长N高M,'S'表示悟空所在地,'T' 表示唐僧所在地,'.'i表示 ...

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

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

  8. 【HRBUST - 1621】迷宫问题II (bfs)

    题干: 小z身处在一个迷宫中,小z每分钟可以走到上下左右四个方向的相邻格之一.迷宫中有一些墙和障碍物. 同时迷宫中也有一些怪兽,当小z碰到任意一个怪兽时,小z需要将怪兽消灭掉才可以离开此方格.但消灭 ...

  9. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <Journey to t ...

  10. HDU 5025 Saving Tang Monk(广州网络赛D题)

    HDU 5025 Saving Tang Monk 题目链接 思路:记忆化广搜,vis[x][y][k][s]表示在x, y结点,有k把钥匙了,蛇剩余状态为s的步数,先把图预处理出来,然后进行广搜即可 ...

最新文章

  1. 算法----单链表反转
  2. C++编程语言之赋值运算符
  3. Windows 全部调试符号包下载
  4. TensorFlow 中文文档 介绍
  5. VTK:标记关键点用法实战
  6. 区块链跨链领域新突破!
  7. 通用唯一标识码UUID的介绍及使用
  8. LeetCode每日打卡 - 4的幂
  9. Go泛型草案设计简明指南
  10. [对象转原始类型总结] ('' + obj) === `${obj}`? 不一定!
  11. 全网首发:研究WORD布局,会意之笑
  12. Atitit.css 规范 bem 项目中 CSS 的组织和管理
  13. 2003系统虚拟dns服务器向导,安装和管理dns服务器.doc
  14. linux越狱时手机怎么进入dfu,通过软件恢复进入DFU刷机模式教程
  15. IEEE transactions 的Latex模板入门笔记
  16. 双色球网页历史数据爬取
  17. 下载并安装Pandoc
  18. 新东方尹圆圆老师的博客上找来的
  19. 用数学计算1.01的365次方等于37.8来说明积跬步至千里是否合理? 1.01^365=37.8 0.99^365=0.03
  20. 2019年一定要去缅甸看一看,景色美到让人哭

热门文章

  1. Puzzle UVA - 227 谜题
  2. 关于使用VS2015编译项目时出现LNK1112 module machine type 'x64' conflicts with target machine type 'X86'
  3. 基于Babylon.js编写宇宙飞船模拟程序1——程序基础结构、物理引擎使用、三维罗盘
  4. SQL Server索引 (原理、存储)聚集索引、非聚集索引、堆 第一篇
  5. vue项目移动端、pc端适配方案(px转rem)
  6. 数据分析师职业发展的几个层次,具体是什么做什么的
  7. marlin固件烧录教程_Marlin固件的步进电机控制代码解析
  8. 11 月全国程序员平均工资出炉
  9. 体验谷歌菜市场镜像版
  10. 广东法院公开裁判文书超350万份