Saving Tang Monk
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 155 Accepted Submission(s): 59
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.
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.
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
Source
大致题意:K是孙悟空,T是唐僧,S是蛇精,数字代表钥匙,从K到T必须要依次集齐k个钥匙,#不能走,过 . 花一分钟时间,过S需要杀死蛇精两分钟,蛇精只用杀死一次,最多5个。
大致题解:bfs,一个状态是x,y(当前坐标),k(拿到第几个钥匙),state(杀了哪些蛇),总共是n^2*k*2^m(m是蛇个数)个状态,因为第一次过蛇是两步,所以当前节点是蛇且还没杀时停留一次,这样就保证最后第一次广搜到T的结果是最短的,具体见代码。
代码:
/**
* @author neko01
*/
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int N=105;
int n,k,m;
struct node{
int x,y;
int k;
int state;
int step;
};
int sx,sy;
int have[10];
int dir[4][2]={-1,0,1,0,0,-1,0,1};
bool vis[N][N][11][40];
int id[N][N];
char s[N][N];
bool inmap(int x,int y)
{
return x>=0&&x<n&&y>=0&&y<n&&s[x][y]!='#';
}
int bfs()
{
queue<node>q;
node cur,next;
cur.x=sx,cur.y=sy;
cur.step=0;
cur.state=0;
cur.k=0;
clr(vis);
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
if(s[cur.x][cur.y]=='S')
{
int idnow=id[cur.x][cur.y];
if(!(cur.state&(1<<idnow)))
{
int nowstate=cur.state|(1<<idnow);
if(!vis[cur.x][cur.y][cur.k][nowstate])
{
vis[cur.x][cur.y][cur.k][nowstate]=1;
next.x=cur.x,next.y=cur.y,next.k=cur.k;
next.state=nowstate;
next.step=cur.step+1;
q.push(next);
}
continue;
}
}
for(int i=0;i<4;i++)
{
int xx=cur.x+dir[i][0];
int yy=cur.y+dir[i][1];
if(inmap(xx,yy))
{
if(s[xx][yy]=='0'+cur.k+1)
{
if(!vis[xx][yy][cur.k+1][cur.state])
{
vis[xx][yy][cur.k+1][cur.state]=1;
next.x=xx,next.y=yy,next.k=cur.k+1;
next.state=cur.state;
next.step=cur.step+1;
q.push(next);
}
}
else
{
if(!vis[xx][yy][cur.k][cur.state])
{
vis[xx][yy][cur.k][cur.state]=1;
next.x=xx,next.y=yy,next.k=cur.k;
next.state=cur.state;
next.step=cur.step+1;
q.push(next);
}
}
if(s[xx][yy]=='T'&&next.k==k)
return next.step;
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
if(n==0&&k==0) break;
m=0;
clr(id);
clr(have);
for(int i=0;i<n;i++)
{
scanf("%s",s[i]);
for(int j=0;j<=n;j++)
{
if(s[i][j]=='K')
sx=i,sy=j;
if(s[i][j]>='0'&&s[i][j]<='9')
have[s[i][j]-'0']=1;
if(s[i][j]=='S')
{
id[i][j]=m;
m++;
}
}
}
int flag=0;
for(int i=1;i<=k;i++)
if(have[i]!=1)
{
flag=1;
break;
}
if(flag==1)
{
puts("impossible");
continue;
}
int ans=bfs();
if(ans==-1)
puts("impossible");
else
printf("%d\n",ans);
}
return 0;
}