作业题 Saving Tang Monk

A: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

sol:
注意到蛇只有5条,状压一下,钥匙只有9个,并且只能按顺序取,只要记取到第几个钥匙即可,状态全了,做一下最短路就好了。注意到蛇会使你的时间多1,考虑到用最短路来做这道题可能会超时(我也没算复杂度),就用最简单的距离为1的bfs做点修改就好了,因为维护的单调的队列中,只有距离为1和2的边,我们记disnow为当前队头的dis,则disnow-1的点扩展中遇到蛇的情况会在disnow即将+1的时候被加入队列,这样就维护了队列的单调性,使得每个点只会入队一次。
比想象中好写很多

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int dx[]={
     0,0,-1,1};
const int dy[]={
     1,-1,0,0};
const int N=110;
const int M=N*N*10*(1<<5);
int T,n,m;
int dis[N][N][10][1<<5],vis[N][N][10][1<<5];//key snake
int snake[N][N];
class cc
{
     
	public:
	int x,y,key,op;
	cc(int x_,int y_,int key_,int op_) {
     x=x_;y=y_;key=key_;op=op_;}
	cc() {
     }
};
cc h[M];//维护h的单增性
vector<cc> past_q,now_q;
char sr[N][N];
bool isnum(int x)
{
     
	return x>=0&&x<=9;
}
bool jud(int x,int y)
{
     
	return sr[x][y]!='#'&&x<=n&&x>=1&&y<=n&&y>=1;
}
int main()
{
     
	while(1)
	{
     
		cin>>n>>m;
		if(n==0&&m==0) break;
		
		int Kx,Ky,cnt=0,disnow=0;
		memset(vis,0,sizeof(vis));
		memset(dis,0,sizeof(dis));
		memset(snake,0,sizeof(snake));
		past_q.clear();
		now_q.clear();
		
		for(int i=1;i<=n;++i)
		{
     
			scanf("%s",sr[i]+1);
			for(int j=1;j<=n;++j)
			{
     
				if(sr[i][j]=='K')
				{
     
					Kx=i;
					Ky=j;
				}
				if(sr[i][j]=='S')
					snake[i][j]=++cnt;
			}
		}
		
		int ans=-1;
		int t=0,w=1;
		cc now;
		h[w]=(cc){
     Kx,Ky,0,0};
		vis[Kx][Ky][0][0]=1;
		while(t<w||!past_q.empty()||!now_q.empty())
		{
     
			++t;
			now=h[t];
			if(t>w||dis[now.x][now.y][now.key][now.op]>disnow)
			{
     
				if(!past_q.empty())
				{
     
					for(auto it=past_q.begin();it!=past_q.end();++it)
					{
     
						if(!vis[it->x][it->y][it->key][it->op])
						{
     
							vis[it->x][it->y][it->key][it->op]=1;
							dis[it->x][it->y][it->key][it->op]=disnow+1;
							h[++w]=*it;
						}
					}
				}
				past_q.clear();
				past_q=now_q;
				now_q.clear();
				disnow++;
			}
			if(t>w)
			{
     
				--t;
				continue;
			}
			now=h[t];
			for(int k=0;k<=3;++k)
			{
     
				cc tmp=now;
				tmp.x+=dx[k];
				tmp.y+=dy[k];
				if(!jud(tmp.x,tmp.y)) continue;
				if(sr[tmp.x][tmp.y]=='T'&&now.key==m)
				{
     
					ans=dis[now.x][now.y][now.key][now.op]+1;
					break;
				}
				if(isnum(sr[tmp.x][tmp.y]-'0'))
				{
     
					if(tmp.key==sr[tmp.x][tmp.y]-'0'-1)
						tmp.key++;
				}
				bool bite=0;
				if(snake[tmp.x][tmp.y]&&!(tmp.op&(1<<snake[tmp.x][tmp.y]-1)))
				{
     
					bite=1;
					tmp.op|=(1<<snake[tmp.x][tmp.y]-1);
					if(!vis[tmp.x][tmp.y][tmp.key][tmp.op])
					now_q.push_back(tmp);
				}
				if(!bite&&!vis[tmp.x][tmp.y][tmp.key][tmp.op])
				{
     
					vis[tmp.x][tmp.y][tmp.key][tmp.op]=1;
					dis[tmp.x][tmp.y][tmp.key][tmp.op]=disnow+1;
					h[++w]=tmp;
				}
			}
			
			
			if(ans!=-1) break;
		}
		if(ans==-1) printf("impossible\n");
		else printf("%d\n",ans);
	}
}

你可能感兴趣的:(作业题 Saving Tang Monk)