题目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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; int n,m;
char s[][];
int b[][][];
int t[][]={,,,,-,,,-};
struct Node{
int x,y,B,s;
friend bool operator<(Node a,Node b)
{
return a.s>b.s;
}
}node;
priority_queue<Node> q; void bfs(int x,int y){
node.x=x;node.y=y;
node.B=;node.s=;
q.push(node);
b[x][y][]=;
int f=-;
while(q.size()){ for(int i=;i<;i++){
int tx=q.top().x+t[i][];
int ty=q.top().y+t[i][];
if(tx<||ty<||tx>n||ty>m) continue;
int tB=q.top().B;
int ts=q.top().s+;
if(s[tx][ty]=='#'&&tB<) continue;
if(s[tx][ty]=='#'){
tB--;
ts++;
}
if(s[tx][ty]=='B'&&tB<) tB++;
if(s[tx][ty]=='P') ts--;
if(s[tx][ty]=='T'){
f=ts;
break;
}
if(b[tx][ty][tB]==) continue;
b[tx][ty][tB]=;
node.x=tx;node.y=ty;
node.B=tB;node.s=ts;
q.push(node);
}
if(f>-) break;
q.pop();
}
printf("%d\n",f);
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)&&n+m){
for(i=;i<=n;i++){
scanf(" %s",s[i]+);
}
while(q.size()){
q.pop();
}
memset(b,,sizeof(b));
int f=;
for(i=;i<=n;i++){
for(j=;j<=m;j++){
if(s[i][j]=='S'){
bfs(i,j);
f=;
break;
}
}
if(f==) break;
}
}
return ;
}

ACM-ICPC2018北京网络赛 Saving Tang Monk II(bfs+优先队列)的更多相关文章

  1. Saving Tang Monk II(bfs+优先队列)

    Saving Tang Monk II https://hihocoder.com/problemset/problem/1828 时间限制:1000ms 单点时限:1000ms 内存限制:256MB ...

  2. 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, ...

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

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

  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. Saving Tang Monk II HihoCoder - 1828 2018北京赛站网络赛A题

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

  7. ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

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

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

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

  9. hihocoder #1828 : Saving Tang Monk II(BFS)

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

随机推荐

  1. 关于js全局变量数组push数据时dom中无数据的问题

    今天着实悲催,这问题整了好几个小时才解决.废话不多说,上问题. 一开始我定义了许多全局变量放在me下. var me = { dgOrderDetails: null, dgVisitNumbers: ...

  2. VM tools安装错误The path "" is not a valid path to the xx generic kernel headers.

    VMWARE TOOLS安装提示THE PATH IS NOT A VALID PATH TO THE GENERIC KERNEL HEADERSI solved this problem, I g ...

  3. terminal中 启动ios模拟器,并安装软件

    启动运行模拟器: xcrun instruments -w 'iPhone 6 Plus' 在已经启动好的模拟器中安装应用: xcrun simctl install booted Calculato ...

  4. 【shell】awk引用外部变量

    在使用awk的过程中,经常会需要引用外部变量,但是awk需要使用单引号将print包起来,导致print后的$引用无效,可以采用下面的方式 例如: #!/bin/bash a="line1 ...

  5. 微信小程序开发:学习笔记[5]——JavaScript脚本

    微信小程序开发:学习笔记[5]——JavaScript脚本 快速开始 介绍 小程序的主要开发语言是 JavaScript ,开发者使用 JavaScript 来开发业务逻辑以及调用小程序的 API 来 ...

  6. joomla搬家之后打不开 首页404错误

    joomla 安装好之后, 网站打不开,首页404错误,后台能够正常访问,数据库连接正常.应该是 nginx配置的问题.该如何修改配置呢?随便一个链接点进去都是404,找不到页面,URL的形式如下: ...

  7. matlab之scatter3()函数

    Display point cloud in scatter plot(在散点图中显示点云): scatter3(X,Y,Z) 在向量 X.Y 和 Z 指定的位置显示圆圈. scatter3(X,Y, ...

  8. HTML font: 12px/1.5 Arial; 是什么意思

    意思是: 字体12像素 行高 1.5em 字体 Arial 可以参考下面资料: 我们常用的font属性有下面这六种: font-style设定斜体 如:font-style: italic; font ...

  9. hdu-5795 A Simple Nim(组合游戏)

    题目链接: A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  10. PS 色调— —颜色梯度

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...