Saving Tang Monk II

https://hihocoder.com/problemset/problem/1828

时间限制: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

比赛的时候犯傻了,一直TLE。。。多加一维数组表示当前拿到的氧气瓶的数量

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<cstdio>
 8 using namespace std;
 9
10 char map[105][105];
11 int book[105][105][15];
12 int dir[4][2]={0,1,1,0,0,-1,-1,0};
13 int n,m;
14 struct sair{
15     int x,y,s,b;
16     friend bool operator<(sair a,sair b){
17         return a.s>b.s;
18     }
19 };
20
21 void bfs(int x,int y){
22     sair s,e;
23     s.x=x,s.y=y,s.b=s.s=0;
24     priority_queue<sair>Q;
25     Q.push(s);
26     while(!Q.empty()){
27         s=Q.top();
28         Q.pop();
29         for(int i=0;i<4;i++){
30             e.x=s.x+dir[i][0];
31             e.y=s.y+dir[i][1];
32             e.s=s.s+1;
33             e.b=s.b;
34             if(e.x>=0&&e.x<n&&e.y>=0&&e.y<m){
35                 if(map[e.x][e.y]=='#'){
36                     if(e.b){
37                         e.b--;
38                         e.s++;
39                     }
40                     else{
41                         continue;
42                     }
43                 }
44                 if(map[e.x][e.y]=='P'){
45                     e.s--;
46                 }
47                 if(map[e.x][e.y]=='B'&&e.b<5){
48                     e.b++;
49                 }
50                 if(map[e.x][e.y]=='T') {
51                     printf("%d\n",e.s);
52                     return;
53                 }
54                 if(!book[e.x][e.y][e.b]){
55                     book[e.x][e.y][e.b]=1;
56                     Q.push(e);
57                 }
58             }
59         }
60     }
61     printf("-1\n");
62     return;
63 }
64
65 void solve(){
66     memset(book,0,sizeof(book));
67     for(int i=0;i<n;i++){
68         for(int j=0;j<m;j++){
69             if(map[i][j]=='S'){
70                 bfs(i,j);
71                 return;
72             }
73         }
74     }
75 }
76
77 int main(){
78     while(~scanf("%d %d",&n,&m)){
79         if(!n&&!m) break;
80         for(int i=0;i<n;i++){
81             scanf("%s",&map[i]);
82         }
83         solve();
84     }
85 }

View Code

转载于:https://www.cnblogs.com/Fighting-sh/p/9708465.html

Saving Tang Monk II(bfs+优先队列)相关推荐

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

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

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

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

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

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

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

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

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

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

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

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

  7. 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)

    1 /* 2 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 3 以前做过的都是用二维的!自己的四维还是太狭隘了..... 4 5 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找 ...

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

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

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

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

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

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

最新文章

  1. mongodb 入门
  2. texmaker不能点击跳转到行
  3. blockquote 引用的分析
  4. CSS3实现页面的平滑过渡
  5. 如何专业化监控一个Kubernetes集群?
  6. 能不做自己写个类,也叫java.lang.String
  7. 禅道启动mysql报错_测试工具之在Linux服务器上部署禅道Bug管理系统
  8. SAP License:SAP权限对象文集
  9. 游戏中的影子制作技术
  10. python matplotlib 绘图操作
  11. 监控web状态的脚本
  12. linux下给qt4安装QSerialPort
  13. H5神兽,优优,牛来了,牛小天,牛欢喜,至尊星空等搭建教程
  14. 小米人APK改之理(APK IDE)
  15. win10系统解决Svn图标不显示的问题
  16. Python-Level2-day04:正则表达式概述,元字符使用,匹配规则(特殊字符匹配,贪与非贪婪模式,分组),re模块使用
  17. 展锐Android-Q LCD调试
  18. 第二篇:Cydia添加源和安装软件
  19. 神经网络 和 NLP —— 语言模型和词向量
  20. Java两个线程交替打印奇偶数(两种方法对比)

热门文章

  1. 凡刻(Fenke)FK169机械手表测评
  2. Android 最常用的设计模式五 安卓源码分析——建造者模式
  3. 8、ESP8266 深度睡眠
  4. linux文件夹的执行权限不够怎么解决
  5. NIOS_Altera EPCS下载 注意事项20210815
  6. vue项目实现pc端适配
  7. 数据分析师职业发展的几个层次,具体是什么做什么的
  8. 2019ug最新版本是多少_UGNX将在2019年发布最新版本,让人更意想不到的是它的这项新功能!...
  9. android View的生命周期
  10. CText更新至V1.1.0