做法:优先队列模板题,按步数从小到大为优先级,PASS掉曾经以相同氧气瓶走过的地方就好了

题目1 : Saving Tang Monk II

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

《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 #include <iostream>
  2 #include <queue>
  3 #include <cstring>
  4 using namespace std;
  5
  6 char mp[110][110];
  7 bool vis[110][110][10000];
  8 int d[4][2] = { { 1,0 },{ -1,0 },{ 0,1 },{ 0,-1 } };
  9 int x, y;
 10
 11 struct node
 12 {
 13     int x, y;
 14     int step;
 15     int o;
 16     bool operator < (const node &a) const {
 17         return step > a.step;
 18     }
 19 };
 20
 21
 22 int bfs(node st)
 23 {
 24     priority_queue<node> q;
 25     st.step = 0;
 26     st.o = 0;
 27     q.push(st);
 28     node now, next;
 29
 30     while (!q.empty())
 31     {
 32         now = q.top();
 33         q.pop();
 34
 35         for (int i = 0; i < 4; i++)
 36         {
 37             next = now;
 38             next.step++;
 39             next.x += d[i][0];
 40             next.y += d[i][1];
 41             if (next.x < 0 || next.y < 0 || next.x >= x || next.y >= y)
 42                 continue;
 43             if (next.o == 0 && mp[next.x][next.y] == '#')
 44                 continue;
 45
 46             if (mp[next.x][next.y] == 'T')
 47             {
 48                 return next.step;
 49
 50             }
 51
 52             if (mp[next.x][next.y] == 'P')
 53                 next.step--;
 54             else if (mp[next.x][next.y] == '#')
 55             {
 56                 next.step++;
 57                 next.o--;
 58             }
 59             else if (mp[next.x][next.y] == 'B')
 60             {
 61                 if(next.o < 5)
 62                     next.o++;
 63             }
 64
 65             if (vis[next.x][next.y][next.o])
 66                 continue;
 67
 68
 69             vis[next.x][next.y][next.o] = 1;
 70             q.push(next);
 71         }
 72     }
 73     return -1;
 74 }
 75
 76 int main()
 77 {
 78     ios::sync_with_stdio(false);
 79     cin.tie(0);
 80     cout.tie(0);
 81     while (cin >> x >> y && (x || y))
 82     {
 83         memset(vis, 0, sizeof(vis));
 84         memset(mp, '.', sizeof(mp));
 85         node st;
 86         for (int i = 0; i < x; i++)
 87             for (int j = 0; j < y; j++)
 88             {
 89                 cin >> mp[i][j];
 90                 if (mp[i][j] == 'S')
 91                 {
 92                     st.x = i;
 93                     st.y = j;
 94                 }
 95             }
 96         cout << bfs(st) << endl;
 97     }
 98
 99     return 0;
100 }

转载于:https://www.cnblogs.com/qq965921539/p/9690548.html

ACM-ICPC 2018北京网络赛-A题 Saving Tang Monk II-优先队列相关推荐

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

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

  2. 2011 ACM/ICPC 福州赛区网络赛解题报告

    第一次写网络赛的题解,福州赛区网络赛作为我第一年ACM最后一次网络赛酱油,画了一个很像逗号的句号.....好吧,还得为北京现场赛准备啊准备....... 这次酱油打的很犀利,貌似出第一题很快,之后节奏 ...

  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. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A. Saving Tang Monk II

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

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

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

  7. 2014 ACM/ICPC 鞍山赛区网络赛(清华命题)

    为迎接10月17号清华命题的鞍山现场赛 杭电上的题目 Biconnected(hdu4997)     状态压缩DP Rotate(hdu4998)    相对任一点的旋转 Overt(hdu4999 ...

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

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

  9. hihoCoder1228(2015北京网络赛B题)

    题意: 给出一个文本编辑器的容量,给出老板输入的字符串,小写字母代表文本,大写字母代表命令: L:光标左移: R:光标右移: S:在insert模式和另一个输入模式中切换: D:删除光标后面的一个字符 ...

  10. 2015北京网络赛 G题 Boxes bfs

    Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonl ...

最新文章

  1. oracle is删除表数据库,rman删除expired备份提示“This command is forbidden”
  2. 如何配置jenkins 与代理服务器吗?
  3. 程序员所应具备的品质
  4. 1.0 深度学习回顾与PyTorch简介 - PyTorch学习笔记
  5. spring beanFactory
  6. 串口服务器工作方式及常见异常故障问题排除方法介绍
  7. 蓝桥杯 ALGO-100 算法训练 整除问题
  8. My 1st webUI try
  9. etcd nginx 容器_Etcd+confd实现动态修改nginx文件
  10. ThinkPHP5集成JS-SDK实现微信自定义分享功能
  11. [Pytorch系列-57]:循环神经网络 - gensim.models.word2vec参数详解与构建词向量模型
  12. vue-ES2015:
  13. 学生体质健康测试成绩测算软件,国家学生体质健康测试成绩自动生成模板
  14. oracle10gwin,win10系统没有法安装Oracle10g如何办?
  15. 基于MATLAB的图像分割系统
  16. ifrme嵌入外部页面,在外部页面调用本页面方法,window.postMessage实现跨域通信
  17. java 自动论坛评论,Java论坛系统巡云轻论坛
  18. APP如何变现?主流变现方式有这些
  19. vt口令服务器找到但是没有找到口令信息,数控程序仿真软件VT打不开的几个问题解决方案...
  20. html制作小短片,如何制作视频短片

热门文章

  1. python apkg_python解包wxapkg_GitHub - python6460/wxappUnpacker: Wechat App(微信小程序,.wxapkg)解包及相关...
  2. oracle11g查看数据库名称,oracle11g系列 事物和常用数据库对象
  3. 微信机器自动问答机器人
  4. PS图层蒙版、参考线显示边距、盖印图层
  5. java 对数运算_使用java计算log值
  6. 数据经济时代大数据四大发展趋势
  7. vue项目实现pc端适配
  8. CText更新至V1.1.0
  9. 云服务器选择、腾讯云轻量应用服务器面板介绍
  10. unity实现mmd功能(跳舞)