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

Input

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.

Output

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

Sample Input

2 2
S#
#T
2 5
SB###
##P#T
4 7
SP.....
P#.....
......#
B...##T
0 0

Sample Output

-1
8
11

题意:给你一个二维字符矩阵,S字符代表孙悟空的位置,T字符代表唐生的位置。现在是孙悟空要用最小的距离去到唐生的位置,路途中有以下几种位置‘#‘ 是毒区,要有一一个氧气才可以进去,并且需要额外消耗一个时间来恢复身体。‘B‘ 是氧气区,进来会拿到一个氧气,每一个氧气区可以无限拿,但是孙悟空身上最多可以拿5个氧气管。’P‘ 是能量区,进来会获得一个能力包,一个能量包可以让你行动一次不消耗时间。’.‘ 空区域,任意行走。思路:开一个思维的dis数组来记录以下状态dis[x][y][o][p] 表示 在x,y位置,有o个氧气,p个能力包时的最小用时。然后用BFS正常搜即可,细节处理下即可。

提示:每一个能量包能用就用的话可以让代码写起来更简单。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n, m;
char s[200][234];
struct node
{
    int x, y, step, o, p;
    node() {}
    node(int xx, int yy, int oo, int pp, int ss)
    {
        x = xx;
        y = yy;
        o = oo;
        p = pp;
        step = ss;
    }
};
int xx[] = { -1, 1, 0, 0};
int yy[] = {0, 0, -1, 1};
int dis[104][123][7][5];
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    while (~scanf("%d %d", &n, &m))
    {
        if (n + m == 0)
        {
            break;
        }
        repd(i, 1, n)
        {
            scanf("%s", s[i] + 1);
        }
        int sx, sy;
        repd(i, 1, n)
        {
            repd(j, 1, m)
            {
                if (s[i][j] == ‘S‘)
                {
                    sx = i;
                    sy = j;
                    break;
                }
            }
        }
        queue<node> q;
        q.push(node(sx, sy, 0, 0, 0));
        memset(dis, inf, sizeof(dis));
        // cout<<dis[0][2][2][1]<<endl;
        int ans = inf;
        while (!q.empty())
        {
            node temp = q.front();
            q.pop();
            repd(i, 0, 3)
            {
                int tx = temp.x + xx[i];
                int ty = temp.y + yy[i];
                if (tx >= 1 && tx <= n && ty >= 1 && ty <= m)
                {
                    if (s[tx][ty] == ‘T‘)
                    {
                        if (temp.p > 0)
                        {
                            dis[tx][ty][temp.o][temp.p] = min(dis[tx][ty][temp.o][temp.p], temp.step);
                            ans = min(dis[tx][ty][temp.o][temp.p], ans);
                        } else
                        {
                            dis[tx][ty][temp.o][temp.p] = min(dis[tx][ty][temp.o][temp.p], temp.step + 1);
                            ans = min(dis[tx][ty][temp.o][temp.p], ans);
                        }
                    } else if (s[tx][ty] == ‘B‘)
                    {
                        // get oxy
                        if (temp.o >= 5)
                        {
                            if (temp.p > 0)
                            {
                                if (dis[tx][ty][temp.o][temp.p] > temp.step)
                                {
                                    dis[tx][ty][temp.o][temp.p] = temp.step;
                                    q.push(node(tx, ty, temp.o, temp.p - 1, temp.step));
                                }
                            } else
                            {
                                if (dis[tx][ty][temp.o][temp.p] > temp.step + 1)
                                {
                                    dis[tx][ty][temp.o][temp.p] = temp.step + 1;
                                    q.push(node(tx, ty, temp.o, temp.p, temp.step + 1));
                                }
                            }
                        } else
                        {
                            if (temp.p > 0)
                            {
                                if (dis[tx][ty][temp.o][temp.p] > temp.step)
                                {
                                    dis[tx][ty][temp.o][temp.p] = temp.step;
                                    q.push(node(tx, ty, temp.o + 1, temp.p - 1, temp.step));
                                }
                            } else
                            {
                                if (dis[tx][ty][temp.o][temp.p] > temp.step + 1)
                                {
                                    dis[tx][ty][temp.o][temp.p] = temp.step + 1;
                                    q.push(node(tx, ty, temp.o + 1, temp.p, temp.step + 1));
                                }
                            }
                        }
                    } else if (s[tx][ty] == ‘P‘)
                    {
                        // get p
                        if (temp.p > 0)
                        {
                            if (dis[tx][ty][temp.o][temp.p] > temp.step)
                            {
                                dis[tx][ty][temp.o][temp.p] = temp.step;
                                q.push(node(tx, ty, temp.o, temp.p, temp.step));
                            }
                        } else
                        {
                            if (dis[tx][ty][temp.o][temp.p] > temp.step + 1)
                            {
                                dis[tx][ty][temp.o][temp.p] = temp.step + 1;
                                q.push(node(tx, ty, temp.o, temp.p + 1, temp.step + 1));
                            }
                        }
                    } else if (s[tx][ty] == ‘#‘)
                    {
                        // du qu
                        if (temp.o > 0)
                        {
                            if (temp.p > 0)
                            {
                                if (dis[tx][ty][temp.o][temp.p] > temp.step + 1)
                                {
                                    dis[tx][ty][temp.o][temp.p] = temp.step + 1;
                                    q.push(node(tx, ty, temp.o - 1, temp.p - 1, temp.step + 1));
                                }
                            } else
                            {
                                if (dis[tx][ty][temp.o][temp.p] > temp.step + 2)
                                {
                                    dis[tx][ty][temp.o][temp.p] = temp.step + 2;
                                    q.push(node(tx, ty, temp.o - 1, temp.p, temp.step + 2));
                                }
                            }
                        }
                    } else
                    {
                        // nomal
                        if (temp.p > 0)
                        {
                            if (dis[tx][ty][temp.o][temp.p] > temp.step)
                            {
                                dis[tx][ty][temp.o][temp.p] = temp.step;
                                q.push(node(tx, ty, temp.o, temp.p - 1, temp.step));
                            }
                        } else
                        {
                            if (dis[tx][ty][temp.o][temp.p] > temp.step + 1)
                            {
                                dis[tx][ty][temp.o][temp.p] = temp.step + 1;
                                q.push(node(tx, ty, temp.o, temp.p, temp.step + 1));
                            }
                        }
                    }
                }
            }
        }
        if (ans == inf)
        {
            printf("-1\n");
        } else
        {
            printf("%d\n", ans );
        }
    }

    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ‘ ‘ || ch == ‘\n‘);
    if (ch == ‘-‘) {
        *p = -(getchar() - ‘0‘);
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 - ch + ‘0‘;
        }
    }
    else {
        *p = ch - ‘0‘;
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 + ch - ‘0‘;
        }
    }
}


原文地址:https://www.cnblogs.com/qieqiemin/p/10807177.html

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

Saving Tang Monk II HihoCoder - 1828 2018北京赛站网络赛A题的相关文章

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,

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

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

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

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

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

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

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

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

[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

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>