做法:优先队列模板题,按步数从小到大为优先级,PASS掉曾经以相同氧气瓶走过的地方就好了

题目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 <iostream>
#include <queue>
#include <cstring>
using namespace std; char mp[][];
bool vis[][][];
int d[][] = { { , },{ -, },{ , },{ ,- } };
int x, y; struct node
{
int x, y;
int step;
int o;
bool operator < (const node &a) const {
return step > a.step;
}
}; int bfs(node st)
{
priority_queue<node> q;
st.step = ;
st.o = ;
q.push(st);
node now, next; while (!q.empty())
{
now = q.top();
q.pop(); for (int i = ; i < ; i++)
{
next = now;
next.step++;
next.x += d[i][];
next.y += d[i][];
if (next.x < || next.y < || next.x >= x || next.y >= y)
continue;
if (next.o == && mp[next.x][next.y] == '#')
continue; if (mp[next.x][next.y] == 'T')
{
return next.step; } if (mp[next.x][next.y] == 'P')
next.step--;
else if (mp[next.x][next.y] == '#')
{
next.step++;
next.o--;
}
else if (mp[next.x][next.y] == 'B')
{
if(next.o < )
next.o++;
} if (vis[next.x][next.y][next.o])
continue; vis[next.x][next.y][next.o] = ;
q.push(next);
}
}
return -;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
while (cin >> x >> y && (x || y))
{
memset(vis, , sizeof(vis));
memset(mp, '.', sizeof(mp));
node st;
for (int i = ; i < x; i++)
for (int j = ; j < y; j++)
{
cin >> mp[i][j];
if (mp[i][j] == 'S')
{
st.x = i;
st.y = j;
}
}
cout << bfs(st) << endl;
} return ;
}

ACM-ICPC 2018北京网络赛-A题 Saving Tang Monk II-优先队列的更多相关文章

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

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

  2. 北京2018网络赛 hihocoder#1828 : Saving Tang Monk II (BFS + DP +多开一维)

    hihocoder 1828 :https://hihocoder.com/problemset/problem/1828 学习参考:https://www.cnblogs.com/tobyw/p/9 ...

  3. ACM ICPC 2018 青岛赛区 部分金牌题题解(K,L,I,G)

     目录: K Airdrop I Soldier Game L Sub-cycle Graph G Repair the Artwork ———————————————————— ps:楼主脑残有点严 ...

  4. HDU 5033 Building(北京网络赛B题) 单调栈 找规律

    做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time L ...

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

  6. hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)

    时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device w ...

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

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

  8. 2014 ACM/ICPC 鞍山赛区网络赛(清华命题)

    为迎接10月17号清华命题的鞍山现场赛 杭电上的题目 Biconnected(hdu4997)     状态压缩DP Rotate(hdu4998)    相对任一点的旋转 Overt(hdu4999 ...

  9. ACM-ICPC 2018青岛网络赛-H题 Traveling on the Axis

    题目:略(不知道怎么从ZOJ搬题) 地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4054 把这题的每个点分成两种情况 ...

随机推荐

  1. unity自带寻路Navmesh入门教程

    http://liweizhaolili.blog.163.com/blog/static/16230744201271161310135/ http://liweizhaolili.blog.163 ...

  2. sqlserver计算日期

    在网上找到的一篇文章,相当不错哦O(∩_∩)O~ 这是计算一个月第一天的SQL 脚本:  SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一 ...

  3. Sqlserver2008 FileStream解决图片存储问题

    SQLserver FileStream的出现就是为了解决对大对象的存储中一个矛盾. 对于图片的存储方式 第一种:方式是存储在数据库里面,这种方式一般使用image字段,或者varbinary(max ...

  4. 面向对象的JavaScript-009-闭包

    引自:https://developer.mozilla.org/cn/docs/Web/JavaScript/Closures 闭包是指能够访问自由变量的函数 (变量在本地使用,但在闭包中定义).换 ...

  5. Luogu 4245 【模板】任意模数NTT

    这个题还有一些其他的做法,以后再补,先记一下三模数$NTT$的方法. 发现这个题不取模最大的答案不会超过$10^5 \times 10^9 \times 10^9 = 10^{23}$,也就是说我们可 ...

  6. name相同获取值

    <html> <head> </head> <body> <form name="form1" method="po ...

  7. 彻底修改Eclipse的默认编码

    引用各位前辈经验得到彻底修改eclipse默认编码的方法. 单在eclipse里设置编码方式非常复杂且容易遗漏,全部修改后,有些代码生成模板内的${encode}变量仍为原编码方案,经过查阅许多资料得 ...

  8. mongo学习-稀疏索引

    因为,如果要创建唯一索引,那么如果这个值有好几个为Null的,所以也会导致我们创建索引失败,那么我们可以引进系数索引这个概念,它可以做到,支持如果值存在的情况,它必须是唯一的,我们可以 将 uniqu ...

  9. [GO]使用select实现斐波那契

    package main import "fmt" func fibonacci(ch chan <- int, quit <- chan bool) { x, y : ...

  10. 关于Markdown格式转PDF格式

    Markdown转PDF格式 个人使用的Markdown编辑平台:有道云笔记网页版 当我们编辑好自己的随笔以后,在网页的[客户端下载]下面有一个[更多]的圆形图标选项,点击后在菜单中有一处[打印]选项 ...