当前位置: 首页 > 工具软件 > Monk.Node > 使用案例 >

HDU5025 Saving Tang Monk(BFS)

郎子平
2023-12-01

《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

 

题目大意:给定一个字符矩阵,每个字符代表一个房间,K代表起点,T代表终点,S代表房间里面有蛇,要多花费1个单位的时间杀掉,最多有五条蛇。#代表障碍,不能进入。房间里面可能有钥匙,每个房间最多一把,钥匙最多9种,用数字表示。获得钥匙必须按照123456789的顺序,得到所有种类的钥匙之后到达终点则结束,求走出迷宫的最短时间。

思路:直接bfs,但是因为有蛇的房间可能会多花1个单位的时间,所以用优先队列,每次pop出时间最短(时间相同则为钥匙编号最大)的状态,然后上下左右走。每走到一个状态都记录这个状态中蛇有没有被杀死,如果被杀死了重复走进去就不用再多花费时间了。

题上说蛇最多只有5条,刚开始写的时候把这个条件忽略了= =

 

#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)
    {
    	if(a.step!=b.step)
    		return a.step>b.step;
    	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==n)
			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;
}

 

 类似资料: