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

题意:孙悟空在S点,唐僧在T点。孙悟空要去救唐僧。一路上有很多房间, ' . '代表的是普通房间,B代表的是有氧气瓶的房间,P代表的是有加速药的房间,#代表毒气室。孙悟空在没有氧气瓶的情况下不能进入毒气室,在有氧气瓶的情况下,进入毒气室后需要休息一个单位时间。孙悟空最多携带五个氧气瓶,但是可以携带无数个加速药丸。加速药丸可以使下一次移动不费时间(相当于进入P房间无需时间)。

问最需要多少时间可以救出唐僧,如果不行,输出-1;

采用分层bfs, 把氧气瓶数量看成一个特征量。这样相当于一个三维移动点(x,y,b) b是氧气瓶数量。

因为加速药丸可以无限携带,所以不作为特征量。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 1e2 +5;
const int INF = 0x3f3f3f3f;
int X[4] = {0,0,-1,1};
int Y[4] = {1,-1,0,0};
char str[MAXN][MAXN];
int  vis[10][MAXN][MAXN];
int n,m,sx,sy;
struct Node
{int x,y,t,b;///t是时间,b是氧气瓶数目bool operator<(const Node &a)const///优先队列返回所用时间少的那一个{return t>a.t;}
};
priority_queue<Node>q;
void init()
{M(vis,0);while(!q.empty())q.pop();
}
int bfs(int x,int y)
{q.push({x,y,0,0});while(!q.empty()){int x1 = q.top().x;int y1 = q.top().y;int t1 = q.top().t;int b1 = q.top().b;q.pop();if(vis[b1][x1][y1]==0)vis[b1][x1][y1] =1;elsecontinue;for(int i=0; i<4; i++){int xx = x1 +X[i];int yy = y1 +Y[i];if(xx>=1&&xx<=n&&yy>=1&&yy<=m){if(str[xx][yy] =='T'){return t1+1;}else if(str[xx][yy] == '.'||str[xx][yy] == 'S')///普通房间,时间+1{q.push({xx,yy,t1+1,b1});}else if(str[xx][yy] == '#' && b1>0)///有氧气瓶的情况下进入毒气室,时间+2,氧气瓶数-1{q.push({xx,yy,t1+2,b1-1});}else if(str[xx][yy] == 'B')///进入氧气瓶室,氧气瓶数+1,但不大于5。{q.push({xx,yy,t1+1,min(5,b1+1)});}else if(str[xx][yy] == 'P')///进入加速药丸室,不花费时间。{q.push({xx,yy,t1,b1});}}}}return -1;
}
int main()
{while(scanf("%d %d",&n,&m)&&n&&m){init();for(int i=1; i<=n; i++){scanf("%s",str[i]+1);}for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){if(str[i][j]=='S' )///确定起点{sx = i;sy = j;}}}int ans = bfs(sx,sy);printf("%d\n",ans);}return  0;
}

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

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

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

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

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

  3. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 【bfs + 记忆化搜索 + 剪枝】 AC 代码

    ACM 北京区域赛 bfs+剪枝+ms 第一个一遍过的题目,基本没有看题解 记忆搜索当中,注意初始化成一个特殊值:而在访问之后,每个点就会有一个不同于INF(或者 -1等特殊标记)的值 先到先得,适者 ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 Tomb Raider(map+二进制枚举)

    #1829 : Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daugh ...

  5. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(双向队列+尺取法)

    #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules ...

  6. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D【队列】

    #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules ...

  7. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D. 80 Days

    题解 题目大意 n个点组成一个环形 初始钱为m 从i走到j需要-b[i] + a[j] 要求按照顺时针走完所有的点(不用再回到起点) 过程中m不能小于0 输出最小的起点编号 直接把a[i]和b[i]合 ...

  8. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛

    Saving Tang Monk II Tomb Raider Cheat 80 Days Odd Chess Shortest Path Problem The Mole K-Dimensional ...

  9. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days ——尺取

    描述 80 Days is an interesting game based on Jules Verne's science fiction "Around the World in E ...

最新文章

  1. Windows 编译cython nms
  2. fast-rcnn练习资料整理
  3. div+css中设置了float属性后如何让外层的高度随着内层的高度大小自动调整
  4. C语言工程实践-简单文本编辑器
  5. 如何ASP.NET Core Razor中处理Ajax请求
  6. WebApp 里Meta标签大全
  7. pytorch学习笔记(1):开始一个简单的分类器
  8. 目标检测(十九)--SoftNMS
  9. TIGER(泰格)胡东国老师带单有盈利吗?平台正规吗?
  10. CDR绘制抖音APP图标
  11. 而立之年,路漫漫,修其强大
  12. 第1章:QLableButty
  13. Hadoop 百度百科
  14. python求圆周率马青公式_Python 实现丘德诺夫斯基(Chudnovsky)法計算高精度圓周率...
  15. 苹果手机怎么投屏?图文教程,轻松学会
  16. 高薪程序员必备—Redis高性能缓存数据库
  17. 利用Abot爬虫和visjs 呈现漫威宇宙
  18. c语言对sht11编程,SHT11数字温湿度传感器 含源代码和仿真图
  19. dp怎么接显示器和主机_电脑显卡接口科普,怎么连接主机和显示器
  20. 如果逃离北上广深,我们可以去哪里?

热门文章

  1. 【偏振光1】什么是偏振光
  2. tensorflow基础知识(CNN)
  3. python爬虫——爬取起点中文网作品信息
  4. 如何实现一个深拷贝(考虑循环引用对象、和symbol类型)
  5. 【Redis高级应用总结】
  6. 列表, 元组, range() 知识总结
  7. 7个管理学常用工具:SWOT、PDCA、6W2H、SMART、WBS、二八原则
  8. 【转帖】针尖对麦芒?学林 IHIFI 812 VS QLS QA350 V2
  9. 计算机毕业设计ssm贵工程线上拍卖平台的设计与实现c1jil系统+程序+源码+lw+远程部署
  10. 夸奖对方代码写的好_夸奖别人画得好怎么写