题目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. opencv使用记录

    /*2017-1-14*/ /*视频的读取...*/ int g_n=0; void on_change(int pos,void *)//看来void*不能省! { printf("g_n ...

  2. 【题解】 AT2134 Zigzag MST

    [题解]AT2134 Zigzag MST 一道MST好题 \(Anson\)有云: 要么是减少边的数量. 要么是改变连接边的方式. 那么如何减少边的数量呢?很简单,把所有不可能对答案产生贡献的边去掉 ...

  3. django mysql setting 设置

    django mysql setting  django mysql 设置 DATABASES = {    'default': {        'ENGINE': 'django.db.back ...

  4. rails debug

    =debug @thesis config下配置 东西需要重启之后才管用

  5. AFNetworking3.0使用简介

    注意到咱们集成的版本为3.0.4. 下面的类已从AFNetworking 3.0中废弃: AFURLConnectionOperation AFHTTPRequestOperation AFHTTPR ...

  6. YxdJSON - Delphi 高性能 JSON 库(支持RTTI和序列化操作)

    源:YxdJSON - Delphi 高性能 JSON 库(支持RTTI和序列化操作) Delphi 高性能 JSON 库(支持RTTI和序列化操作) 支持平台: Windows, Android, ...

  7. Eclipse for PHP Developers使用笔记

    1 修改字体样式:Window-->Preference-->General-->Appearance-->Basic-->text font-->edit

  8. 玩转Google开源C++单元测试框架Google Test系列(gtest)(总)

    原文地址:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Goo ...

  9. python学习笔记:第五天( 字典)

    Python3 字典 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格 ...

  10. Android Studio 字体大小和背景色的设置

    豆绿色的RGB值:#C7EDCC 1.打开Android Studio——>Ctrl+Alt+s 或者 File——>Settings 2.在弹窗中选中“Colors&Fonts” ...