Saving Tang Monk

《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. But to rescue Tang Monk, Sun Wukong might need to get some keys and kill some snakes in his way.

The palace can be described as a matrix of characters. Each character stands for a room. In the matrix, 'K' represents the original position of Sun Wukong, 'T' represents the location of Tang Monk and 'S' stands for a room with a snake in it. Please note that there are only one 'K' and one 'T', and at most five snakes in the palace. And, '.' means a clear room as well '#' means a deadly room which Sun Wukong couldn't get in.

There may be some keys of different kinds scattered in the rooms, but there is at most one key in one room. There are at most 9 kinds of keys. A room with a key in it is represented by a digit(from '1' to '9'). For example, '1' means a room with a first kind key, '2' means a room with a second kind key, '3' means a room with a third kind key... etc. To save Tang Monk, Sun Wukong must get ALL kinds of keys(in other words, at least one key for each kind).

For each step, Sun Wukong could move to the adjacent rooms(except deadly rooms) in 4 directions(north,west,south and east), and each step took him one minute. If he entered a room in which a living snake stayed, he must kill the snake. Killing a snake also took one minute. If Sun Wukong entered a room where there is a key of kind N, Sun would get that key if and only if he had already got keys of kind 1,kind 2 ... and kind N-1. In other words, Sun Wukong must get a key of kind N before he could get a key of kind N+1 (N>=1). If Sun Wukong got all keys he needed and entered the room in which Tang Monk was cuffed, the rescue mission is completed. If Sun Wukong didn't get enough keys, he still could pass through Tang Monk's room. Since Sun Wukong was a impatient monkey, he wanted to save Tang Monk as quickly as possible. Please figure out the minimum time Sun Wukong needed to rescue Tang Monk.

Input There are several test cases.

For each case, the first line includes two integers N and M(0 < N <= 100, 0 <= M <= 9), meaning that the palace is a N * N matrix and Sun Wukong needed M kinds of keys(kind 1, kind 2, ... kind M).

Then the N*N matrix follows.

The input ends with N = 0 and M = 0. Output For each test case, print the minimum time (in minute) Sun Wokong needed to save Tang Monk. If it's impossible for Sun Wokong to complete the mission, print "impossible". Sample Input
3 1
K.S
##1
1#T
3 1
K#T
.S#
1#.
3 2
K#T
.S.
21.
0 0
Sample Output
5
impossible
8

题目大意:唐僧被白骨精抓走了,被关在一个迷宫里,孙悟空要去救他。迷宫由很多房间组成,有的房间有致命伤害,而有的房间则是干净的。有些房间里面有钥匙,然而孙悟空必须捡起前N - 1把钥匙才能捡起第N把钥匙,
比如现在房间里有3号钥匙,那么你必须有1号钥匙和2号钥匙才能捡起3号钥匙,孙悟空必须收集完全部M把钥匙才能解救唐僧。悟空每分钟能够走一个房间,但是如果进入的房间有蛇的话,悟空必须花1分钟杀掉蛇,蛇被杀死之后不会再出现。
求悟空就出唐僧的最短时间。 解题思路:由于会多花一分钟杀蛇导致移动时间不同,所以BFS要用优先队列。由于最多有5只蛇,所以我们可以用A、B、C、D、E来标记这5条蛇,然后判断这条蛇是否被杀过。
另一个条件就是钥匙,我们可以将它放到结构体中来记录当前钥匙的编号,如果当前钥匙的号比房间里的钥匙编号少1,那么就可以更新钥匙的值。
复制代码
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

struct node
{
    int x, y;
    int step;//表示的是花费时间 
    int key;//表示拥有的钥匙 
    bool snack[6];//表示的是访问节点的次数 
    friend bool operator < (node a, node b)//当返回为true的时候,说明B优先 
    {
        if(a.step!=b.step)
            return a.step>b.step;//谁步数小谁优先,步数相同大key优先 
        return a.key<b.key;
    }
}start, over;
 
int n, m;//m个钥匙 n*n
char st[105][105];
bool used[105][105][10];
int dx[4]={-1, 0, 1, 0};
int dy[4]={0, -1, 0, 1};
 
bool in(node a)
{
    return a.x>=1 && a.x<=n && a.y>=1 && a.y<=n;
}
 
int bfs()
{
    priority_queue <node> q;//小顶堆 。重载运算符<决定是小顶堆 
    while(!q.empty()) q.pop();
    start.step=start.key=0;
    memset(start.snack, 0, sizeof(start.snack));
    q.push(start);
    node a, b;
    while(!q.empty())
    {
        a=q.top();
        q.pop();
        if(a.x==over.x && a.y==over.y && a.key==m)
            return a.step;
        for(int i=0; i<4; i++)
        {
            b.x=a.x+dx[i];
            b.y=a.y+dy[i];
            b.key=a.key;
            if(in(b) && st[b.x][b.y]!='#' && !used[b.x][b.y][b.key])
            {
                for(int j=0; j<6; j++) b.snack[j]=a.snack[j];
                b.step=a.step+1;
                used[b.x][b.y][b.key]=1;
                if(b.x==over.x && b.y==over.y && b.key==m)
                    return b.step;
                else
                {
                    if(st[b.x][b.y]>0 && st[b.x][b.y]<6 && !b.snack[st[b.x][b.y]])
                    {
                        b.snack[st[b.x][b.y]]=1;
                        b.step++;
                    }
                    else if(st[b.x][b.y]-'0'==a.key+1)
                    {
                        b.key++;
                    }
                    q.push(b);
                }
            }
        }
    }
    return -1;
}
 
int main()
{
    int num;
    while(cin>>n>>m && n+m)
    {
        num=1;
        memset(used, 0, sizeof(used));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                cin>>st[i][j];
                if(st[i][j]=='K')
                {
                    start.x=i, start.y=j;
                    used[i][j][0]=1;
                }
                else if(st[i][j]=='T')
                {
                    over.x=i, over.y=j;
                }
                else if(st[i][j]=='S')
                {
                    st[i][j]=num;
                    num++;
                }
            }
        }
        int ans=bfs();
        if(ans==-1) cout<<"impossible"<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}
复制代码

 

posted @   BlackSnow  阅读(193)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示