[状态压缩 & 广搜BFS]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.

输入

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.

输出

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".

样例输入

3 1
K.S
##1
1#T
3 1
K#T
.S#
1#.
3 2
K#T
.S.
21.
0 0

样例输出

5
impossible
8
解题分析

这道题可以算是广搜的天花板之一了,不仅需要状态压缩的策略,还需要使用一些形如priority_queue的数据结构来辅助,而且关于已访问过的标记,包括拿到的钥匙,杀死的蛇等等细节都很值得推敲。以及说,我们可以用一个数组来存储我们钥匙的状态,并且我们考虑的是其二进制的表达式。首先,正常地读入数据,注意,由于我们可能有很多组数组,所以每次的while循环的开始,我们应该重置我们需要重置的一些变量,比如说访问数组归0,蛇计数数组归0,答案ans的重置。接着读入整个迷宫,需要注意的是,我们需要孙悟空的初始位置,以及我们读入蛇的时候,把这条蛇的信息放入一个数组里面,并且标记它的位置。对于我们广搜时放入priority_queue的节点的话,我们希望它有我们的位置,杀死蛇的情况,钥匙情况以及我们的步数,位置坐标和步数是很基本的。注意,我们每次的广搜,只走一步!所以我们在一次的循环里不能直接更改我们当前读取的队列开头的元素的信息,所以另设变量去处理是一个很好的选择。一共三种情况,要么是钥匙,要么是蛇,要么就是唐僧,分情况,利用位操作去判断,去处理,用&运算取出某一位,用|运算去把某一位设置为1,诸如此类,这般这般。最后输出答案即可。

代码演示
#include <iostream>
#include <cmath>
//#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>
#include <queue>
#include <stack>
#include <cstdlib>
#define INF (1<<30)
using namespace std;

int N,M;
char maze[105][105];
bitset<513> vis_keys[105][105];
bitset<33> vis_snakes[105][105];
int ans=INF;
int Kx,Ky,k;
int keys[]={0,1,3,7,15,31,63,127,255,511};
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};

struct Node{
	int x,y,snakes,keys,step;
	bool operator<(const Node& o) const{
		return step>o.step;
	}
};

struct snake{
	int x,y;
} snakes[5];
Node tmp;

int bfs(){
	priority_queue<Node> q;
	q.push({Kx,Ky,0,0,0});
	while(q.size()){
		tmp=q.top();
		q.pop();
		for(int i=0;i<4;i++){
			int x0=tmp.x+dx[i];
			int y0=tmp.y+dy[i];
			if(x0<0 || y0<0 || x0>=N || y0>=N || maze[x0][y0]=='#') continue;
			if(vis_keys[x0][y0][tmp.keys]==1 && vis_snakes[x0][y0][tmp.snakes]==1) continue;
			int nsnakes=tmp.snakes,nkeys=tmp.keys,nstep=tmp.step+1;
			if(isdigit(maze[x0][y0])){
				int m=maze[x0][y0]-'0';
				if(m==1 || (nkeys>>(m-2)) & 1){
					nkeys |= (1<<(m-1));
				}
			}
			else if(maze[x0][y0]=='S'){
				int index=0;
				for(int i=0;i<k;i++){
					if(snakes[i].x==x0 && snakes[i].y==y0){
						index=i;
						break;
					}
				}
				if(!((nsnakes>>index) & 1)){
					nsnakes |=(1<<index);
					nstep +=1;
				}
			}
			else if(maze[x0][y0]=='T' && nkeys==keys[M]){
				return nstep;
			}
			q.push({x0,y0,nsnakes,nkeys,nstep});
			vis_keys[x0][y0][nkeys]=1;
			vis_snakes[x0][y0][nsnakes]=1;
		}
	}
	return -1;
}

int main(){
	while(scanf("%d%d",&N,&M)!=EOF){
		if(N==0 && M==0) break;
		memset(vis_keys,0,sizeof(vis_keys));
		memset(vis_snakes,0,sizeof(vis_snakes));
		k=0;
		ans=1<<30;
		for(int i=0;i<N;i++)
			for(int j=0;j<N;j++){
				scanf(" %c",&maze[i][j]);
				if(maze[i][j]=='K'){
					Kx=i;
					Ky=j;
				}
				else if(maze[i][j]=='S'){
					snakes[k++]={i,j};
				}
			}
		ans=bfs();
		if(ans==-1){
			printf("impossible\n");
		}
		else{
			printf("%d\n",ans);
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值