• 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
    ", BFS()); 
    91     }
    92     return 0;
    93 }
  • 相关阅读:
    Andriod调试桥
    抓包工具charles的使用
    测试常用工具
    Indentation error codes
    Cmder 中文乱码的解决方法
    修改Cmder命令提示符
    统计单词出现的字数
    将字串内容输出到文件
    python数据实例str
    python语法检查工具
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/9695988.html
Copyright © 2020-2023  润新知