• 2018 北京网络赛


    描述

    《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

    题意 : 题目大意 给一个图S是起点 T是终点 .是空房间 #是毒气室 B是氧气瓶存放室 P是加速室 每次走到空房间或者起点消耗1秒 走到氧气室获得一个氧气瓶最多携带5个氧气瓶 进入毒气室需要一瓶氧气并且消耗2秒 进入加速室不消耗时间(可以这么理解) 问S走到T的最短时间

    思路分析 : 正常的分层广搜即可,有个地方坑死我了,就是从S的位置去搜索,但是在搜的时候判断条件里忘记加 S 的判断条件了,只要在开始广搜的时候讲 S 换成 . 即可

    代码示例:

    using namespace std;
    #define ll long long
    int n, m;
    char mp[105][105];
    int sx, sy;
    
    struct node
    {
        int x, y;
        int yang, bu;
        bool operator< (const node &v)const {
            return bu > v.bu;
        }    
    };
    priority_queue<node>que;
    bool vis[105][105][10];
    int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
    
    bool check(int x, int y){
        if (x >= 1 && x <= n && y >= 1 && y <= m) return true;
        return false;
    }
    
    void bfs(){
        while(!que.empty()) que.pop();
        memset(vis, false, sizeof(vis));
        
        que.push({sx, sy, 0, 0});    
        node f;
        mp[sx][sy] = '.'; 
        while(!que.empty()){
            node v = que.top();
            que.pop();
            
            if (mp[v.x][v.y] == 'T') {
                printf("%d
    ", v.bu);
                return;
            }
            if (vis[v.x][v.y][v.yang]) continue;
            vis[v.x][v.y][v.yang] = 1;
            
            for(int i = 0; i < 4; i++){
                int fx = v.x+dir[i][0];
                int fy = v.y+dir[i][1];
                if (check(fx, fy)){
                    if (mp[fx][fy] == '.' || mp[fx][fy] == 'T') {
                        if (!vis[fx][fy][v.yang]) que.push({fx, fy, v.yang, v.bu+1});
                    }
                    else if (mp[fx][fy] == '#'){
                        if (v.yang == 0) continue;
                        if (!vis[fx][fy][v.yang-1]) que.push({fx, fy, v.yang-1, v.bu+2});
                    }
                    else if (mp[fx][fy] == 'P'){
                        if (!vis[fx][fy][v.yang]) que.push({fx, fy, v.yang, v.bu});
                    }
                    else if (mp[fx][fy] == 'B'){ 
                        if (v.yang+1 <= 5 && !vis[fx][fy][v.yang+1]) que.push({fx, fy, v.yang+1, v.bu+1});
                    }
                } 
            }       
        }
        printf("-1
    ");
    }
    
    int main() {
    
        while(~scanf("%d%d", &n, &m) && m+n){
            
            for(int i = 1; i <= n; i++){
                scanf("%s", mp[i]+1);
                for(int j = 1; j <= m; j++){
                    if (mp[i][j] == 'S') sx = i, sy = j;
                }
            }
            
            bfs();
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    [数据库]Oracle数据迁移至HIVE(待续)
    [Java EE]解决浏览器跨域问题
    [Linux]Xmanager+Xshell远程管理桌面版CentOS物理服务器的桌面版CentOS虚拟机
    JAVA生成随机数工具类RandomStringUtils详解
    electron 安装不同的版本的方法
    Git 常用操作(一)
    openresty 简单入门
    Ubuntu 分辨率设置
    javascript尾调用与尾递归
    深入理解 JavaScript 执行上下文和执行栈
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/9693461.html
Copyright © 2020-2023  润新知