无法拯救我的菜----北京网络赛 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, 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

思路:bfs+优先队列
被坑死了,我原先写的每次遇到T没有弹出,又进队列了,这样不行,到T之后就得弹出,坑了两个小时

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 105;

int n,m;
bool vis[N][N][N];
char s[N][N];
int col[5] = {0,0,1,-1};
int con[5] = {1,-1,0,0};
typedef struct Node{
    int x,y;
    int ox;
    int num;
    friend bool operator < (const Node &p,const Node &q)
    {
        return p.num > q.num;
    }
}Node;

bool judge(int i,int j)
{
    if(i < 0 || j < 0)
        return false;
    if(i >= n || j >= m)
        return false;
    return true;
}

int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        if(n == 0 && m == 0)
            break;
        priority_queue<Node>que;
        getchar();
        int ex,ey;
        for(int i = 0;i < n;++i)
        {
            scanf("%s",s[i]);
            for(int j = 0;j < m;++j)
            {
                if(s[i][j] == 'S') que.push((Node){i,j,0,0});
                if(s[i][j] == 'T') ex = i;ey = j;
            }
        }
        //cout << ex << " " << ey << endl;
        int MIN = inf;
        memset(vis,false,sizeof(vis));
        while(!que.empty())
        {
            Node tmp = que.top();
            que.pop();
            bool flag = false;
            int x = tmp.x,y = tmp.y;
            if(vis[x][y][tmp.ox]) continue;
            vis[x][y][tmp.ox] = true;
            //cout << x << " " << y << " " << tmp.ox << " " << tmp.se << " " << tmp.num << endl;
            for(int i = 0;i < 4;++i)
            {
                int x1 = x + col[i],y1 = y + con[i];
                //cout << x1 << " " << y1 << endl;
                if(judge(x1,y1)){
                    if(s[x1][y1] == '#'){
                        if(tmp.ox >= 1){
                            que.push((Node){x1,y1,tmp.ox - 1,tmp.num + 2});
                        }
                    }
                    if(s[x1][y1] == 'B'){
                        que.push((Node){x1,y1,((tmp.ox + 1) > 5) ? 5 : (tmp.ox + 1),tmp.num + 1});
                    }
                    if(s[x1][y1] == 'P'){
                        que.push((Node){x1,y1,tmp.ox,tmp.num});
                    }
                    if(s[x1][y1] == 'S' || s[x1][y1] == '.'){
                        que.push((Node){x1,y1,tmp.ox,tmp.num + 1});
                    }
                    if(s[x1][y1] == 'T'){
                        flag = true;
                        MIN = tmp.num + 1;
                        break;
                    }
                }
            }
            if(flag) break;
        }
        if(MIN == inf){
            printf("-1\n");
        }else{
            printf("%d\n",MIN);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值