hdu5025

Saving Tang Monk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1889    Accepted Submission(s): 671


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

题意:孙悟空救师傅,一个n*n的网格,K表示孙悟空,T表示唐僧,#表示墙,. 表示路,数字表示钥匙,S表示蛇怪,;

规则:孙悟空可以上下左右移动,每移动一格花费1分钟时间,如果遇到蛇怪,打死蛇怪另外花费1分钟,最多有5个蛇怪,孙悟空要集齐所有钥匙才能就出师傅,但是取钥匙有一个规则,只有取到钥匙N,才能去钥匙N+1,孙悟空除了墙以外,任何位置都可以到达,唐僧的位置也可以去,但是钥匙没有集齐也是徒劳,(可能唐僧被一个有M种不同的锁的笼子关着呢)M最大为9,

这个地图中钥匙不唯一,一把钥匙可能有在好几个地方都有出现,这样就增加了难度,

优先队列+bfs做此题,构造结构体,里面包括5个值,(x,y)表示下标,t表示花费时间,snake表示这趟路杀死的蛇怪的状态,

k表示现在手里有的最大钥匙值;

解释snake值表示方法,二进制11111表示杀死了五只蛇怪,1表示杀死蛇怪,从右往左数第几个位置是1表示第几个蛇怪被杀死

输入地图给蛇怪编号1~s,比如杀死第i只蛇怪,现在snake状态要+ 1<<i,自己用笔模拟一下就明白;

 

总结: 这道题的关键之处在于状态的表示,1,用vis[i][j][k]三维数组表示(i,j)位置拥有第k个钥匙的状态,也就是说一个位置有最多10种状态,在有0把钥匙的情况下,即使遍历全图也能找到第一把钥匙,所以每个位置每种状态最多经历一次,只有手里的钥匙代号比当前位置钥匙代号小1 时才能把当前位置的钥匙放兜里。

2,蛇怪的生死状态,snake值,举一个例子,snake=6的二进制是110,它表示第2,第3只蛇怪被杀死,如果现在遇到了第一只蛇怪(第一只蛇怪代号为0)1<<0=1,6&1(110&001)相与==0,那么这只蛇怪第一次遇到,要花费2分钟走这一个格子,如果遇到了第二只蛇怪,1<<1=2,6&2(110&010)=2(010),那么这只蛇怪已经在我之前走过的路上被打死,花费1分钟走过这个格子。

具体看代码:

#include <iostream>
#include <queue>
#include <string.h>
#include <stdio.h>
using namespace std;
const int maxn=115;
char map[maxn][maxn];
struct Point
{
    int x,y;
}d;
struct PT
{
    int x,y,t,snake,key;
    PT()
    {
        ;
    }
    PT(int xx,int yy,int tt,int s,int k)
    {
        x=xx,y=yy,t=tt,snake=s,key=k;
    }
    friend bool operator<(const PT a,const PT b)
    {
        return a.t>b.t;
    }
};
int n,Key;
int dx[4]= {0,-1,0,1};
int dy[4]= {-1,0,1,0};
bool vis[maxn][maxn][15];
int dis[maxn][maxn][15];
int Snake[maxn][maxn];
bool check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=n||map[x][y]=='#')
        return false;
     return true;
}
priority_queue<PT>que;
int bfs(Point start)
{
    int nx,ny;
    memset(vis,false,sizeof(vis));
    nx=start.x,ny=start.y;
    while(!que.empty())
    {
        que.pop();
    }
    vis[nx][ny][0]=true;
    que.push(PT(nx,ny,0,0,0));
    while(!que.empty())
    {
        PT now;
        now=que.top();
        que.pop();
        if(map[now.x][now.y]=='T'&&now.key==Key)  ///钥匙凑齐并且找到唐僧
            return now.t;
        for(int i=0; i<4; i++)
        {
            nx=now.x+dx[i];
            ny=now.y+dy[i];
            bool flag=check(nx,ny);
            if(!flag||vis[nx][ny][now.key])
                continue;
             if(now.key+1+'0'==map[nx][ny])///刚好是下一个要找的钥匙
            {
                    vis[nx][ny][now.key+1]=true;
                    que.push(PT(nx,ny,now.t+1,now.snake,now.key+1));
            }
            else if(map[nx][ny]=='S')///遇到蛇
            {
                if(!(now.snake&(1<<Snake[nx][ny])))///没杀过这条蛇
                {
                    vis[nx][ny][now.key]=true;
                    que.push(PT(nx,ny,now.t+2,now.snake|(1<<Snake[nx][ny]),now.key));
                }
                else
                {
                    vis[nx][ny][now.key]=true;
                    que.push(PT(nx,ny,now.t+1,now.snake,now.key));
                }
            }
            else///其他所有情况都包括....
            {
                vis[nx][ny][now.key]=true;
                que.push(PT(nx,ny,now.t+1,now.snake,now.key));
            }

        }

    }
    return -1;
}
int main()
{
    int tot;
    while(cin>>n>>Key)
    {
        if(n==0)
            break;
        tot=0;
        for(int i=0; i<n; i++)
        {
            cin>>map[i];
            for(int j=0; j<n; j++)
            {
                if(map[i][j]=='K')
                {
                    d.x=i,d.y=j;
                }
                if(map[i][j]=='S')
                {
                    Snake[i][j]=tot++;
                }
            }
        }
        int ans=bfs(d);
        if(ans==-1)
            cout<<"impossible"<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值