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): 2016    Accepted Submission(s): 722

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 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 minutes) Sun Wukong needed to save Tang Monk. If it's impossible for Sun Wukong to complete the mission, print "impossible"(no quotes).

 

 

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

 

 

Source

2014 ACM/ICPC Asia Regional Guangzhou Online

 

 题目大意;


K代表孙悟空,T代表唐僧,S代表蛇。数字1-9代表钥匙编号。#代表墙。

输出从K走到T的最少步数,如果不行,输出impossible。


对于这个题目的规则是这样滴:

1、收集到所有钥匙才能解救唐僧

2、必须依次收集钥匙,如果想收集到7这个钥匙,首先你要有1-6这些个钥匙,如果想收集2这个钥匙,首先你要有1这个钥匙。简单的说,想拿N+1这个编号的钥匙,就要有N这个编号的钥匙。

3、悟空如果走到了蛇所在的格子上边,那么一定要和蛇打一架才能继续走。所以这里耗时+1.打过的蛇,不用重复打。


思路:

1、首先说钥匙的处理。因为钥匙的拿取是有全序关系依赖的,不是随随便便就能直接拿的,所以我们对钥匙的处理还是比较简单的,用结构体队列入结构体,结构体中确定一个变量key,如果走到了编号为7的钥匙,那么首先他需要有6的钥匙.也就是说,如果现在key的值为6,那么key就可以变成7啦 

2、其次说和蛇打架的问题,因为和蛇打架的时候有额外的时间消耗,导致如果直接入队结果会不是最优,所以我们这里使用优先队列来搞定这个问题、

3、最后说当前状态是否和某一条蛇打过架的问题,因为一共最多五条蛇,我们不妨对他们进行编号,0A(第0条蛇),B)(第1条蛇),C,D,E【当然编号是无所谓怎么编号的】那么对于当前蛇当前钥匙状态来说,是否已经打过这条蛇的判定我们在结构体中再加入一个变量tmp来进行这个问题的处理,对于tmp的值我们用十进制来存,假设tmp的值为5,那么对应01串:00101可以表示第0条和第3条蛇已经打过了,也就是打过了AC这两条蛇。

那么如何判断当前蛇是否已经打败过了呢?

根据位运算,我们如果走到了蛇的上边,而且我们已经对蛇进行了编号,如果当前蛇是第0条蛇,tmp的值为5,那么我们可以通过与运算(同1为1,其余为0)来处理这个问题。

5:101 第0条蛇:1<<0,然后我们对其进行&位运算101&001明显不等于0,那么就说明当前状态已经打过这条蛇了,相反,如果位运算的值计算为0,那么说明当前状态这条蛇还没有打过,对于位运算不是很了解的童鞋这块可以自己多枚举几个情况就能理解啦~

4、既然已经了解状态需要控制的有两种,那么也就不难理解vis判重数组要开到四维来搞。vis【x】【y】【key】【tmp】。【因为key、tmp不同的时候,可以重复走一个点。】


AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct zuobiao
{
    int x,y,output,key,tmp;
    friend bool operator <(zuobiao a,zuobiao b)
    {
        return a.output>b.output;
    }
}now,nex;
int conts;
char a[105][105];
int vis[104][104][15][1<<6];
int fx[4]={1,-1,0,0};
int fy[4]={0,0,1,-1};
int n,m,sx,sy;
void bfs(int x,int y)
{
    priority_queue<zuobiao>s;//初始化部分
    now.x=x;
    now.y=y;
    now.output=0;
    now.key=0;
    now.tmp=0;
    s.push(now);
    vis[now.x][now.y][now.key][now.tmp]=1;
    while(!s.empty())
    {
        now=s.top();
        if(a[now.x][now.y]=='T'&&now.key==m)//如果达到目标,输出结果。
        {
            printf("%d\n",now.output);
            return ;
        }
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            nex.key=now.key;
            nex.tmp=now.tmp;
            nex.output=now.output+1;
            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<n&&a[nex.x][nex.y]!='#')//首先一定要能够走到的地方才能继续判断
            {
                if(a[nex.x][nex.y]>='A'&&a[nex.x][nex.y]<='E')//snack遇到了蛇
                {
                    int nowsnack=a[nex.x][nex.y]-'A';
                    nowsnack=(1<<nowsnack);
                    if((nex.tmp&nowsnack)==0)//这条蛇并没有杀过
                    {
                        nex.tmp+=nowsnack;//记录上当前状态这条蛇杀过了
                        nex.output+=1;//并且杀蛇的时候耗时+1
                    }
                    if(vis[nex.x][nex.y][nex.key][nex.tmp]==0)
                    {
                        s.push(nex);
                        vis[nex.x][nex.y][nex.key][nex.tmp]=1;
                    }
                }
                else if(a[nex.x][nex.y]>='1'&&a[nex.x][nex.y]<='9')//key;
                {
                    int nowkey=a[nex.x][nex.y]-'0';
                    if(nex.key==nowkey-1)nex.key=nowkey;
                    if(vis[nex.x][nex.y][nex.key][nex.tmp]==0)
                    {
                        s.push(nex);
                        vis[nex.x][nex.y][nex.key][nex.tmp]=1;
                    }
                }
                else
                {
                    if(vis[nex.x][nex.y][nex.key][nex.tmp]==0)
                    {
                        s.push(nex);
                        vis[nex.x][nex.y][nex.key][nex.tmp]=1;
                    }
                }
            }
        }
    }
    printf("impossible\n");
    return ;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(a,' ',sizeof(a));
        memset(vis,0,sizeof(vis));
        if(n==0&&m==0)break;
        conts=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            for(int j=0;j<n;j++)
            {
                if(a[i][j]=='K')
                {
                    sx=i;sy=j;
                }
                if(a[i][j]=='S')//对蛇编号
                {
                    a[i][j]=conts+'A';
                    conts++;
                }
            }
        }
        bfs(sx,sy);
    }
}
/*
7 2
#######
2SKSS1T
#######
.......
.......
.......
.......
7 2
###2###
1SKSS1T
#######
.......
.......
.......
.......
*/










  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值