zoukankan      html  css  js  c++  java
  • Saving Tang Monk II(bfs+优先队列)

    Saving Tang Monk II

    https://hihocoder.com/problemset/problem/1828

    时间限制: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


    比赛的时候犯傻了,一直TLE。。。多加一维数组表示当前拿到的氧气瓶的数量
     1 #include<iostream>
     2 #include<cstring>
     3 #include<string>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<cstdio>
     8 using namespace std;
     9 
    10 char map[105][105];
    11 int book[105][105][15];
    12 int dir[4][2]={0,1,1,0,0,-1,-1,0};
    13 int n,m;
    14 struct sair{
    15     int x,y,s,b;
    16     friend bool operator<(sair a,sair b){
    17         return a.s>b.s;
    18     }
    19 };
    20 
    21 void bfs(int x,int y){
    22     sair s,e;
    23     s.x=x,s.y=y,s.b=s.s=0;
    24     priority_queue<sair>Q;
    25     Q.push(s);
    26     while(!Q.empty()){
    27         s=Q.top();
    28         Q.pop();
    29         for(int i=0;i<4;i++){
    30             e.x=s.x+dir[i][0];
    31             e.y=s.y+dir[i][1];
    32             e.s=s.s+1;
    33             e.b=s.b;
    34             if(e.x>=0&&e.x<n&&e.y>=0&&e.y<m){
    35                 if(map[e.x][e.y]=='#'){
    36                     if(e.b){
    37                         e.b--;
    38                         e.s++;
    39                     }
    40                     else{
    41                         continue;
    42                     }
    43                 }
    44                 if(map[e.x][e.y]=='P'){
    45                     e.s--;
    46                 }
    47                 if(map[e.x][e.y]=='B'&&e.b<5){
    48                     e.b++;
    49                 }
    50                 if(map[e.x][e.y]=='T') {
    51                     printf("%d
    ",e.s);
    52                     return;
    53                 }
    54                 if(!book[e.x][e.y][e.b]){
    55                     book[e.x][e.y][e.b]=1;
    56                     Q.push(e);
    57                 }
    58             }
    59         }
    60     }
    61     printf("-1
    ");
    62     return;
    63 }
    64 
    65 void solve(){
    66     memset(book,0,sizeof(book));
    67     for(int i=0;i<n;i++){
    68         for(int j=0;j<m;j++){
    69             if(map[i][j]=='S'){
    70                 bfs(i,j);
    71                 return;
    72             }
    73         }
    74     }
    75 }
    76 
    77 int main(){
    78     while(~scanf("%d %d",&n,&m)){
    79         if(!n&&!m) break;
    80         for(int i=0;i<n;i++){
    81             scanf("%s",&map[i]);
    82         }
    83         solve();
    84     }
    85 }
    View Code
  • 相关阅读:
    TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)
    分布式与集群的区别是什么?
    模板函数函数模板 Function Template(C++Primer10)
    注意地方hadoop中的pi值计算
    文件错误关于hibernate中报Duplicate class/entity mapping org.model.User错的问题
    元素序列几个常用排序算法:一
    行语句mysql insert操作详解
    分量入度hdu 3836 Equivalent Sets
    查询语句编写高效SQL语句
    方法元素c语言范式编程之lsearch
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9708465.html
Copyright © 2011-2022 走看看