《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
题意:孙悟空救唐僧,#代表着毒气室,只有有氧气管的时候才能进入,出了毒气室要休息一分钟。B代表着氧气室,最多可以带5瓶氧气。P代表着可以加速一分钟。. 代表着空闲地方。问孙悟空从s到达t的最少时间是多少。
之前还没做到过这样的题目,之前的广搜就是判断这个点有没有走到过,走到就不能走了。但是这个题要是这样做的话就不对了,也非常麻烦。我们用vis[x][y][num]代表着走到(x,y)还剩num瓶氧气。如果到达一个点它的氧气瓶的数量之前出现过,那么这种状态之前也出现过。广搜暴力去找所有的状态,直到找到了唐僧。以为在出了毒气室的时候,需要休息一分钟,就代表着进毒气室需要2分钟,这样的话就需要用到优先队列来维护了。
代码如下:

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
char mp[105][105];
int vis[105][105][1005];
struct node{int x,y,num,step;node(int a,int b,int c,int d){x=a;y=b;num=c;step=d;}bool operator <(const node &a)const {return a.step<step;}
};
int n,m;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int bfs(int sx,int sy){priority_queue<node>q;node k=node(sx,sy,0,0);q.push(k);vis[sx][sy][0]=1;while(!q.empty()){node t=q.top();q.pop();if(mp[t.x][t.y]=='T'){return t.step;}for(int i=0;i<4;i++){int tx=t.x+dir[i][0];int ty=t.y+dir[i][1];if(tx<0||ty<0||tx>=n||ty>=m)continue;if(mp[tx][ty]=='#'&&t.num>0&&!vis[tx][ty][t.num-1]){vis[tx][ty][t.num-1]=1;q.push(node(tx,ty,t.num-1,t.step+2));}else if(mp[tx][ty]=='B'&&!vis[tx][ty][t.num+1]&&t.num+1<=5){vis[tx][ty][t.num+1]=1;q.push(node(tx,ty,t.num+1,t.step+1));}else if(mp[tx][ty]=='B'&&!vis[tx][ty][t.num]&&t.num+1>5){//最多只能携带五瓶vis[tx][ty][t.num]=1;q.push(node(tx,ty,t.num,t.step+1));}else if(mp[tx][ty]=='P'&&!vis[tx][ty][t.num]){vis[tx][ty][t.num]=1;q.push(node(tx,ty,t.num,t.step));}else if((mp[tx][ty]=='.'||mp[tx][ty]=='S'||mp[tx][ty]=='T')&&!vis[tx][ty][t.num]){vis[tx][ty][t.num]=1;q.push(node(tx,ty,t.num,t.step+1));}}}return -1;
}
int main(){while(scanf("%d%d",&n,&m)!=EOF){if(n==0&&m==0)return 0;for(int i=0;i<n;i++){scanf("%s",mp[i]);}memset(vis,0,sizeof(vis));int sx,sy;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(mp[i][j]=='S'){sx=i;sy=j;break;}}}printf("%d\n",bfs(sx,sy));}
}

努力加油a啊,(o)/~

Saving Tang Monk II HihoCoder - 1828(2018北京网络赛三维标记+bfs)相关推荐

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

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

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

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

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

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

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

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

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

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

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

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

  7. 2018北京网络赛 HihoCoder - 1835 K-Dimensional Foil II 计算几何 贪心 二分

    题目链接:https://vjudge.net/problem/HihoCoder-1835 题解:首先我们应该能想到到达图形的距离最近那肯定是垂直过去,也就是坐标变为(x1 - k, x2 - k, ...

  8. ACM-ICPC 2018 北京网络赛:K-Dimensional Foil II

    #1835 : K-Dimensional Foil II 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 "K-Dimensional Foil" i ...

  9. ACM-ICPC 2018 北京网络赛:K-Dimensional Foil II 一题多解

    博客目录 原题 题目链接 #1835 : K-Dimensional Foil II 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 "K-Dimensional ...

最新文章

  1. 时间的玫瑰+但斌的投资思维
  2. jQuery之选项卡的实现
  3. 数字图像处理:第二十二章 基于模型的编码
  4. java 读 xml_Java读写XML代码示例
  5. SAP CRM 订单抬头文本的可编辑性讨论
  6. 【TensorFlow】TensorFlow从浅入深系列之十二 -- 教你深入理解卷积神经网络中的池化层
  7. 虚拟化基础架构Windows 2008篇之2-域用户与域用户组管理
  8. h700通话糊 索尼wi_索尼随身听变种!火爆日本的异形智能 500元最强索尼降噪神器来了...
  9. 左右伸缩_OPPO概念机将至!横向卷轴+左右伸缩,你期待吗
  10. LeetCode 98. 验证二叉搜索树(递归)(迭代)
  11. 《Oracle从入门到精通》
  12. html+css静态页面Demo(参考一唯科技官网)
  13. springsoure.sts下载地址
  14. 海湾gst5000协议号_海湾GST5000主机操作说明
  15. C语言之位操作和整形的补位
  16. iOS7到iOS8 一个通用的横竖屏幕切换总结
  17. python提取图片文字视频教学_Python学习第七天之爬虫的学习与使用(爬取文字、图片、 视频)...
  18. PPT中如何插入指定大小的矩阵
  19. MP4文件批量转码成MP3
  20. 我们都没有迎来决赛---Leo读 不是孙振耀写的职场感言 1

热门文章

  1. java 第十一章总结
  2. sonarqube没有html插件,SonarQube Github插件没有写拉问题的问题
  3. python爬虫天气数据_python爬虫:天气数据的分析
  4. python写一个app接收摄像头传输的视频_使用Python的Flask框架实现视频的流媒体传输...
  5. java dataset flatmap_Flink 系例 之 FlatMap
  6. java颜色gui_Java gui颜色不加载
  7. Swift--数组和字典(二)
  8. Swift--变量和常量
  9. 【汇编】汇编学习入门-系列更新20180705
  10. ASP.NET 例程完全代码版(5)——通过web.config配置数据库连接池