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 operator < (const node& p) const {
        return t > p.t;
    }
};
void bfs() {
    node t = (node) {
        sx, sy, 0, 0
    };
    times[sx][sy][0] = 0;
    priority_queue<node> q;
    q.push(t);
    while(!q.empty()) {
        node u = q.top();
        q.pop();
        int x = u.x, y = u.y, o = u.o, t = u.t;

        if(x == ex && y == ey) {
            ans = min(ans, t);
            return;
        }
        for(int d = 0; d < 4; d++) {
            int tx = x + dir[d][0];
            int ty = y + dir[d][1];
            if(tx < 0 || tx >= n || ty < 0 || ty >= m) //越界
                continue;

            if(G[tx][ty] == ‘#‘) {
                if(o == 0) {
                    continue;
                } else if(times[tx][ty][o-1] > t + 2) {

                    q.push((node) {
                        tx,ty,t+2,o-1
                    });

                    times[tx][ty][o-1] = t + 2;
                }
            } else if(G[tx][ty] == ‘P‘ && times[tx][ty][o] > t) {

                q.push((node) {
                    tx,ty,t,o
                });

                times[tx][ty][o] = t;
            } else if(G[tx][ty] == ‘B‘ && o < 5 && times[tx][ty][o + 1] > t + 1) {

                q.push((node) {
                    tx,ty,t+1,o+1
                });

                times[tx][ty][o + 1] = t + 1;
            } else if(times[tx][ty][o] > t + 1) {

                q.push((node) {
                    tx,ty,t+1,o
                });

                times[tx][ty][o] = t + 1;
            }
        }
    }
}
int main() {

    while(~scanf("%d %d", &n, &m)) {
        if(n == 0)
            break;
        for(int i = 0; i < maxN; i++)
            for(int j = 0; j < maxN; j++)
                for(int k = 0 ; k <= 5; k++)
                    times[i][j][k] = inf;
        for(int i = 0; i < n; i++) {
            scanf("%s", G[i]);
        }
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(G[i][j] == ‘S‘)
                    sx = i, sy = j;
                if(G[i][j] == ‘T‘)
                    ex = i, ey = j;
            }
        }

        ans = inf;
        bfs();
        if(ans == inf) {
            printf("-1\n");
        } else {
            printf("%d\n", ans);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Jadon97/p/9691594.html

时间: 2024-08-29 18:27:27

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

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

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

ACM/ICPC 2018亚洲区预选赛北京赛站网络赛

题意:到一个城市得钱,离开要花钱.开始时有现金.城市是环形的,问从哪个开始,能在途中任意时刻金钱>=0; 一个开始指针i,一个结尾指针j.指示一个区间.如果符合条件++j,并将收益加入sum中(收益可能是负数).不符合就++i,并从sum中退掉收益直到sum>=0;区间长度为n时i的位置就是结果. 正确性证明:假设[a,b]是第一个合法区间.某时刻i,j都<a,i在a左边,因此j不可能扩到b右边(否则[a,b]不是第一个合法区间).只可能j仍落在a左边,或者落到a,b中间.在i<a

2018亚洲区预选赛北京赛站网络赛 C.Cheat

题面 题意:4个人围一圈坐着,每个人13张牌,然后从第一个人开始,必须按照A-K的顺序出牌,一个人出牌后,剩下的人依次可以选择是否质疑他,例如,第一个人现在必须出8(因为按照A-K顺序轮到了),可是他没有或者有,无论如何他会说,我出了x个8,这x张牌就背面朝上的放在桌上,如果有人质疑,才会翻开,然后如果发现这并不是x个8,第一个人就要把桌子上所有的牌收回手上,如果是x个8,这个人就要自己把所有牌收回去,最先出完牌的人,且没有被质疑成功的,就是赢家,输出最后4个人手上的剩下牌.然后给出了4个人的牌

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

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,

[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

2017ICPC北京赛区网络赛 Visiting Peking University(简单思维)

描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0, 1, 2, …, n-1. He plans to spend m consecutive days(2 ≤ m ≤ n)in Beijing. During these m days, he intends to use the first day and another day to vis

hdu 4041 2011北京赛区网络赛F 组合数+斯特林数 ***

插板法基础知识 斯特林数见百科 1 #include<iostream> 2 #include<cmath> 3 #include<cstdio> 4 #include<cstring> 5 #define LL long long 6 #define eps 1e-7 7 #define MOD 1000000007 8 using namespace std; 9 int c[2001][2001]={1},stir2[1005][1005]={1};

2015 ACM-ICPC国际大学生程序设计竞赛北京赛区网络赛 1002 Mission Impossible 6

题目链接: #1228 : Mission Impossible 6 解题思路: 认真读题,细心模拟,注意细节,就没有什么咯!写这个题解就是想记录一下rope的用法,以后忘记方便复习. rope(块状链表)属于SGI STL的一部分,不属于ISO C++标准库,但libstdc++-v3也包含了扩展,在头文件#include<ext/rope>,using namespace __gnu_cxx命名空间中.可以在很短的时间内实现快速的插入.删除和查找字符串. rope.size()    返回