hdoj--5025--Saving Tang Monk(bfs三维标记)

Saving Tang Monk

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


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表示唐僧的位置,途中会有一些钥匙1--m,必须把不同种类的钥匙都拿到至少一把,并且拿到k之前一定要拿到k-1,只有集齐了这些钥匙才可以救唐僧,并且路上会有一些蛇,想要通过的话必须把蛇杀死,杀死蛇花费2点时间,求最少的步数
思路:在结构体中记录每一个点的所有状态,拿到的钥匙,杀死的蛇的数量,杀死的蛇的坐标,以及当前的位置还有步数,然后三维标记,队列要用优先队列
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
char map[110][110];
int vis[110][110][1024],n,m,sx,sy,f;
int a[15]={1,2,4,8,16,32,64,128,256,512,1024,2048};
struct node
{
	int x,y,step,key;
	int flog[12];
	int s;
	int X[12],Y[12];
	friend bool operator < (node s1,node s2)
	{
		return s1.step>s2.step;
	}
}p,temp;
bool judge(node s)
{
	if(s.x<0||s.x>=n||s.y<0||s.y>=n)
	return true;
	if(map[s.x][s.y]=='#'||vis[s.x][s.y][s.key])
	return true;
	return false;
}
void bfs()
{
	priority_queue<node>q;
	while(!q.empty()) q.pop();
	memset(vis,0,sizeof(vis));
	p.s=0;//所有状态初始化 
	p.x=sx;
	p.y=sy;
	p.key=0;//不同的二进制数表示不同的钥匙,相加一定不会重复 
	p.step=0;
	for(int i=0;i<=10;i++)
	p.X[i]=p.Y[i]=0;
	
	for(int i=0;i<=10;i++)
	p.flog[i]=0;//拿到的钥匙 
	
	q.push(p);
	
	vis[p.x][p.y][p.key]=1;
	while(!q.empty())
	{
		p=q.top();
		q.pop();
		
		for(int i=0;i<4;i++)
		{
			temp.s=p.s;//状态转移 
			temp.x=p.x+dx[i];
			temp.y=p.y+dy[i];
			
			for(int j=0;j<=10;j++)
			temp.flog[j]=p.flog[j];
			
			for(int j=0;j<=10;j++)
			{
				temp.X[j]=p.X[j];
				temp.Y[j]=p.Y[j];
			}
			
			temp.key=p.key;
			
			if(judge(temp)) continue;
			if(map[temp.x][temp.y]=='T'&&temp.key==f)
			{
				printf("%d\n",p.step+1);
				return ;//如果遇到了唐僧,并且钥匙收集完毕 
			}
			if(map[temp.x][temp.y]=='S')
			{
				int fl=0;//如果遇到一条蛇,判断是否杀过 
				for(int j=0;j<p.s;j++)
				{
					if(p.X[j]==temp.x&&p.Y[j]==temp.y)
					{
						fl=1;
					}
				}
				if(fl==1) temp.step=p.step+1;//杀过的话步数加一 
				else
				{
					temp.X[temp.s]=temp.x;
					temp.Y[temp.s]=temp.y;
					temp.s++;//没杀过的话杀死,并且记录 
					temp.step=p.step+2;
				}
			}
			else temp.step=p.step+1;
			if(map[temp.x][temp.y]=='1')
			{
				if(!p.flog[map[temp.x][temp.y]-'1'])
				{//如果是第一把钥匙,并且没有,直接获取 
					temp.key+=a[map[temp.x][temp.y]-'1'];
					temp.flog[map[temp.x][temp.y]-'1']=1;
				}
				vis[temp.x][temp.y][temp.key]=1;
				q.push(temp);
			}
			else if(map[temp.x][temp.y]>'1'&&map[temp.x][temp.y]<='9')
			{//如果遇到一个钥匙,判断钥匙是否已经有了,并且判断段上一个有木有 
				if(!p.flog[map[temp.x][temp.y]-'1']&&temp.flog[map[temp.x][temp.y]-'1'-1])
				{
					temp.key+=a[map[temp.x][temp.y]-'1'];
					temp.flog[map[temp.x][temp.y]-'1']=1;
				}
				vis[temp.x][temp.y][temp.key]=1;
				q.push(temp);
			}
			else
			{
				vis[temp.x][temp.y][temp.key]=1;
				q.push(temp);
			}
		}
	}
	printf("impossible\n");
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(n==0&&m==0) break;
		f=0;
		for(int i=0;i<m;i++)
		f+=a[i];//将钥匙的总数变为二进制数 
		memset(map,0,sizeof(map));
		for(int i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(int j=0;j<n;j++)
			{
				if(map[i][j]=='K')
				{
					sx=i;
					sy=j;
				}
			}
		}
		bfs();
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值