HDU 5025 Saving Tang Monk(BFS+状压)

时间:2022-07-14 20:06:07

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5025


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


题意:

给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死的蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐僧,集齐m个钥匙之前可以经过唐僧,集齐x个钥匙以前可以经过x+1,x+2..个钥匙,问最少多少步救到唐僧。

思路:

BFS,每个节点维护四个值:

x,y : 当前坐标

key :已经集齐了key个钥匙

step:已经走了多少步


代码如下:

#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
const int MAXN = 117;
int dir[4][2]= {1,0,0,1,0,-1,-1,0,};
char mm[MAXN][MAXN];
int vis[17][MAXN][MAXN];
struct MM
{
    int x, y;
    int step;
    int key;
    set<pair<int, int > >se;
};

int n, m;

int check(int x, int y)
{
    if(x>=0&&x<n&&y>=0&&y<n)
        return 1;
    return 0;
}
queue<MM>q;
int BFS(int x, int y)
{
    MM next, tt, fro;
    fro.step = 0, fro.key = 0;
    fro.x = x, fro.y = y;
    q.push(fro);
    while(!q.empty())
    {
        tt = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            int xx = tt.x+dir[i][0];
            int yy = tt.y+dir[i][1];
            if(check(xx,yy) && mm[xx][yy]!='#')
            {
                next.se = tt.se;
                if(mm[xx][yy]=='S' && next.se.find(make_pair(xx,yy))==next.se.end())
                {
                    next.step = tt.step+2;
                    next.se.insert(make_pair(xx,yy));
                }
                else
                {
                    next.step = tt.step+1;
                }
                next.x = xx, next.y = yy;
                if(mm[xx][yy] == tt.key+1+'0')
                {
                    next.key = tt.key+1;
                }
                else
                    next.key = tt.key;
                if(next.step<vis[next.key][xx][yy] || vis[next.key][xx][yy]==-1)
                {
                    vis[next.key][xx][yy] = next.step;
                    q.push(next);
                }
            }
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(mm[i][j] == 'T')
            {
                return vis[m][i][j];
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0 && m==0)
            break;
        memset(vis,-1,sizeof(vis));
        while(!q.empty())
        {
            q.pop();
        }
        for(int i = 0; i < n; i++)
        {
            scanf("%s",mm[i]);
        }
        int ans = -1;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(mm[i][j] == 'K')
                {
                    ans = BFS(i,j);
                    //     break;
                }
            }
        }
        if(ans == -1)
        {
            printf("impossible\n");
        }
        else
            printf("%d\n",ans);
    }
    return 0;
}