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。。。多加一维数组表示当前拿到的氧气瓶的数量
 #include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std; char map[][];
int book[][][];
int dir[][]={,,,,,-,-,};
int n,m;
struct sair{
int x,y,s,b;
friend bool operator<(sair a,sair b){
return a.s>b.s;
}
}; void bfs(int x,int y){
sair s,e;
s.x=x,s.y=y,s.b=s.s=;
priority_queue<sair>Q;
Q.push(s);
while(!Q.empty()){
s=Q.top();
Q.pop();
for(int i=;i<;i++){
e.x=s.x+dir[i][];
e.y=s.y+dir[i][];
e.s=s.s+;
e.b=s.b;
if(e.x>=&&e.x<n&&e.y>=&&e.y<m){
if(map[e.x][e.y]=='#'){
if(e.b){
e.b--;
e.s++;
}
else{
continue;
}
}
if(map[e.x][e.y]=='P'){
e.s--;
}
if(map[e.x][e.y]=='B'&&e.b<){
e.b++;
}
if(map[e.x][e.y]=='T') {
printf("%d\n",e.s);
return;
}
if(!book[e.x][e.y][e.b]){
book[e.x][e.y][e.b]=;
Q.push(e);
}
}
}
}
printf("-1\n");
return;
} void solve(){
memset(book,,sizeof(book));
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(map[i][j]=='S'){
bfs(i,j);
return;
}
}
}
} int main(){
while(~scanf("%d %d",&n,&m)){
if(!n&&!m) break;
for(int i=;i<n;i++){
scanf("%s",&map[i]);
}
solve();
}
}

Saving Tang Monk II(bfs+优先队列)的更多相关文章

  1. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II(优先队列广搜)

    #include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN]; ]; int n, m, sx, sy, ex, ...

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

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

  3. ACM-ICPC2018北京网络赛 Saving Tang Monk II(bfs+优先队列)

    题目1 : Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also < ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】

    任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...

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

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

  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. hihocoder 1828 Saving Tang Monk II (DP+BFS)

    题目链接 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great C ...

  8. hdu 5025 Saving Tang Monk(bfs+状态压缩)

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

  9. Saving Tang Monk II HihoCoder - 1828 2018北京赛站网络赛A题

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

随机推荐

  1. java实验——将100-300之间的素数输出

    public class 实验1 { //将100-300之间的素数输出 /** * @param args */ public static void main(String[] args) { / ...

  2. GitLab更新远程分支信息

    项目开发中,当远程分支有变动,有人增加或删除了某些分支,而自己本地没有及时自动刷新出来分支的时候,可以用以下命令来更新以下分支信息 git fetch origin --prune 恩,就酱-

  3. 如何在FreePBX ISO 中文版本安装讯时网关,潮流16FXS 网关和潮流话机

    如何在FreePBX ISO 中文版本安装讯时网关,潮流16FXS 网关和潮流话机摘自:http://www.siplab.cn/?p=664 1)迅时的fxo口网关要注册到asterisk,所以现在 ...

  4. OpenGL 画出雷达动态扫描效果(一)

    最终效果如下所示 Demo下载  http://files.cnblogs.com/xd-jinjian/Debug.zip 源代码下载 http://download.csdn.net/detail ...

  5. Python:23种Pandas核心操作

    Pandas 是一个 Python 软件库,它提供了大量能使我们快速便捷地处理数据的函数和方法.一般而言,Pandas 是使 Python 成为强大而高效的数据分析环境的重要因素之一.在本文中,作者从 ...

  6. solr的multivalued使用说明

    solr的schema.xml配置文件在配置Filed的时候,有个属性: MutiValued:true if this field may containmutiple values per doc ...

  7. pomelo RPC调用时新增字段缺失

    接触pomelo开发一个月,正式开始参与项目开发有10天,遇到很多细节的坑,今天讲讲标题:后端服务器节点之间的rpc调用过程中,返回的数据中新增字段缺失问题. 先讲结果:原因是该rpc调用已经采用了p ...

  8. ES6进一步整理

    内容: 1.变量及赋值 2.函数 3.数组及json 4.字符串 5.面向对象 6.Promise 7.generator 8.模块 1.变量及赋值 (1)ES5变量定义 var:     可以重复定 ...

  9. 关于VS+ImageWatch在线调试问题

    1.使用VS肯定离不开在线调试 2.使用Opencv在VS下进行图像处理,那肯定少不了Image Watch 这两个软件在线调试都存在大坑,弄得精疲力尽才找到解决办法!!! 以下问题都可以通过这个设置 ...

  10. 今天练手了下mysqlbinlog,标记下

    1 首先查看是否开启了 bin log 登录mysql后  使用命令 show variables like "log_%"; show binary logs; 2  确认开启了 ...