Saving Tang Monk
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1591 Accepted Submission(s): 570
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
Source
2014 ACM/ICPC Asia Regional Guangzhou Online
好题。
比赛的时候花了很多时间,最后非要把这题做出来。
题目大意:孙悟空救唐僧。给出一个图。n*n
一共有m把钥匙
里面会有几种元素:1.数字,代表钥匙的序号。2.‘#’,也就是墙,不能走的。3.‘.’,也就是路。4.‘S’,蛇。
每分钟走一步,如果要打死蛇,再加一分钟。
解救唐僧的条件是,孙悟空手上集齐所有m钥匙.并且到达唐僧位置。
在收集钥匙的时候,必须是有序的。比如获取第5号钥匙时,必须已经有了第1,2,3,4把。以此类推。
问最少的时间才能把唐僧救出来。蛇最多有5个。
我的方法是:BFS+状态压缩+优先队列。
使用优先队列每次取当前时间步数最少的。
用5位二进制表示蛇的状态,0表示蛇活着,1表示蛇已经被孙降服(此时可视为‘.’)。
对整个图广搜,主要分为两种情况。
1.当前位置是‘S’,则用位运算判断当前蛇是死是活,死了的话就当‘.’看,活着的话就打死(time+1).
2.当前位置是‘1-9’,判断当前能否获得这把钥匙。key+1==(input[xx][yy]-'0');
#include <stdio.h>
#include <string.h>
#define N 105
#include <queue>
using namespace std;
typedef struct Node
{
int x,y,time,snake,key;
friend bool operator <(Node a,Node b)
{
return a.time>b.time;
}
}Node;
int n,m,k;
int kstart,kend;
char input[N][N];
int vis[N][N][10][40];
int snakes[15];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
priority_queue <Node> q;
int bfs(int x,int y)
{
while(!q.empty())
q.pop();
Node fir ;
fir.x=x;
fir.y=y;
fir.time=0;
fir.snake=0;
fir.key=0;
vis[x][y][0][0]=1;
q.push(fir);
while(!q.empty())
{
Node fro=q.top();
if(input[fro.x][fro.y]=='T'&&fro.key==m)
return fro.time;
q.pop();
for(int i=0;i<4;i++)
{
int xx=fro.x+dx[i];
int yy=fro.y+dy[i];
if(xx<0||xx>=n||yy<0||yy>=n||input[xx][yy]=='#'||vis[xx][yy][fro.key][fro.snake])
continue;
Node nxt;
nxt=fro;
nxt.x=xx;
nxt.y=yy;
nxt.time=fro.time+1;
if(input[xx][yy]>='1'&&input[xx][yy]<='9')
{
int num=input[xx][yy]-'0'-1;
if(nxt.key==num)
nxt.key++;
vis[xx][yy][nxt.key][nxt.snake]=1;
q.push(nxt);
}
else if(input[xx][yy]=='S')
{
for(int i=0;i<k;i++)
{
if(xx==snakes[i]/n&&yy==snakes[i]%n)
{
if((nxt.snake>>i)&1)
{
vis[xx][yy][nxt.key][nxt.snake]=1;
q.push(nxt);
}
else
{
nxt.time++;
nxt.snake+=(1<<i);
vis[xx][yy][nxt.key][nxt.snake]=1;
q.push(nxt);
}
break;
}
}
}
else
{
vis[xx][yy][nxt.key][nxt.snake]=1;
q.push(nxt);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)>0&&(m||n))
{
memset(vis,0,sizeof(vis));
k=0;
for(int i=0;i<n;i++)
scanf("%s",input[i]);
for(int i=0 ;i<n ;i++)
{
for(int j=0;j<n;j++)
{
if(input[i][j]=='K')
kstart=i*n+j;
if(input[i][j]=='T')
kend=i*n+j;
if(input[i][j]=='S')
snakes[k++]=i*n+j;
}
}
int ans=bfs(kstart/n,kstart%n);
if(ans==-1)
puts("impossible");
else
printf("%d\n",ans);
}
return 0;
}