hihocoder #1828 : Saving Tang Monk II(BFS)

描述

《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
 1 /*
 2 堆优化的广搜
 3
 4 需要注意的是使用标记数组记录的是哪些点已经走过了,这里使用三维的标记数组标记哪些点的第几次走过,不再
 5 对其进行拓展。
 6 */
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <queue>
10 using namespace std;
11
12 const int maxn = 100 + 10;
13 int n, m;
14 int sx, sy, ex, ey;
15 char e[maxn][maxn];
16 int nex[4][2] = {0,1, 1,0, 0,-1, -1,0};
17 bool bk[maxn][maxn][10];
18
19 struct Node {
20     int x, y, B, P, s;
21     bool operator < (const Node &a) const {
22         if(a.s == s)
23             return a.B > B;
24         return a.s < s;
25     }
26 };
27
28 int BFS() {
29     memset(bk, 0, sizeof(bk));
30     priority_queue<Node> q;
31     bk[sx][sy][0] = 1;
32     q.push((Node){sx, sy, 0, 0, 0});
33
34     int T = 0;
35     while(!q.empty()) {
36         Node tmp = q.top();
37         q.pop();
38
39         if(e[tmp.x][tmp.y] == ‘T‘)
40             return tmp.s;
41
42         for(int i = 0; i < 4; i++) {
43             int tx = tmp.x + nex[i][0];
44             int ty = tmp.y + nex[i][1];
45
46             if(tx < 1 || tx > n || ty < 1 || ty > m || bk[tx][ty][tmp.B])
47                 continue;
48             bk[tx][ty][tmp.B] = 1;
49
50             if(e[tx][ty] == ‘T‘)
51                 return tmp.s + 1;
52             else if(e[tx][ty] == ‘#‘) {
53                 if(tmp.B > 0) {
54                     q.push((Node){tx, ty, tmp.B - 1, tmp.P, tmp.s + 2});
55                 }
56             }
57             else if(e[tx][ty] == ‘B‘) {
58                 if(tmp.B < 5)
59                     q.push((Node){tx, ty, tmp.B + 1, tmp.P, tmp.s + 1});
60                 else
61                     q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s + 1});
62             }
63             else if(e[tx][ty] == ‘P‘) {
64                 q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s});
65             }
66             else if(e[tx][ty] == ‘.‘) {
67                 q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s + 1});
68             }
69             else if(e[tx][ty] == ‘S‘) {
70                 q.push((Node){tx, ty, tmp.B, tmp.P, tmp.s + 1});
71             }
72         }
73     }
74     return -1;
75 }
76
77 int main()
78 {
79     while(scanf("%d%d", &n, &m) == 2 && n + m != 0) {
80         for(int i = 1; i <= n; i++) {
81             for(int j = 1; j <= m; j++) {
82                 scanf(" %c", &e[i][j]);
83                 if(e[i][j] == ‘S‘) {
84                     sx = i;
85                     sy = j;
86                 }
87             }
88         }
89
90         printf("%d\n", BFS());
91     }
92     return 0;
93 }

原文地址:https://www.cnblogs.com/wenzhixin/p/9695988.html

时间: 2024-08-30 11:52:14

hihocoder #1828 : Saving Tang Monk II(BFS)的相关文章

hihocoder 1828 Saving Tang Monk II (DP+BFS)

题目链接 Problem Description <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

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

#include<bits/stdc++.h> using namespace std; const int maxN = 123; const int inf = 1e9 + 7; char G[maxN][maxN]; int times[maxN][maxN][6]; int n, m, sx, sy, ex, ey, ans; int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; struct node { int x, y, t, o; bool

ACM-ICPC 2018青岛网络赛-A题 Saving Tang Monk II

做法:优先队列 题目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,

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

<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 Mon

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

题面 题意:N*M的网格图里,有起点S,终点T,然后有'.'表示一般房间,'#'表示毒气房间,进入毒气房间要消耗一个氧气瓶,而且要多停留一分钟,'B'表示放氧气瓶的房间,每次进入可以获得一个氧气瓶,最多只能带5个,'P'表示有加速器的房间,进入可以获得一个,可以拥有无限个,然后使用一个可以让你用的时间减一. 题解:对于P房间,也就是到达时不花费时间, 对于#房间,也就是进入前必须要有氧气瓶,消耗的时间为2 对于B房间,氧气瓶数量num=min(num+1,5) 因为有氧气瓶,所以每一个节点状态除

HDU-5025 2014广州网络赛 Saving Tang Monk 状压+BFS

给出一个N*N的矩阵,开启牢门需要收集齐m种钥匙,且必须收集了前i-1种钥匙才能收集第i种钥匙,最终收集齐了回到关押唐僧的房间拯救唐僧,经过一个'S'的房间时需要额外耗时把蛇打死,蛇最多5条,所以状压一下用优先队列BFS求最小时间即可. #include <iostream> #include <cstdio> #include <cmath> #include <queue> #include <vector> #include <cst

[ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 941    Accepted Submission(s): 352 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Clas

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

http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <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

POJ 5025 Saving Tang Monk(状压搜索)

http://acm.hdu.edu.cn/showproblem.php?pid=5025 搜索题:注意蛇的状态(第一次路过要杀掉蛇花2s,第二次以后1s).状态为坐标 ,蛇 加钥匙最大值.用[坐标加拿到最高等级的钥匙]去重. 代码: #include<iostream> #include<queue> #include<map> #include<cstdio> #include<string> #include<cstring>