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
 
题意:K是起点,T是终点S是蛇,#是墙.是路,1-9是钥匙,要收集齐所有的钥匙,才能救出T(收集不全的时候你可以经过T房间)问最短时间
题解:用snack记录蛇是否被杀死,key记录收集到前多少把钥匙,
 
#include<stdio.h>
#include<string.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 101
#define mod 10003
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
int head[MAX],ans;
int n,m,t;
char s[MAX][MAX];
int vis[MAX][MAX][20][1<<6];
int ke[15];
struct node
{
int x,y,key,time,snack;
friend bool operator< (node a,node b)
{
return a.time>b.time;
}
};
int Move[4][2]={1,0,-1,0,0,1,0,-1};
int judge(int a,int b)
{
if(a>=0&&a<n&&b>=0&&b<n&&s[a][b]!='#')
return 1;
return 0;
}
int bfs(int x1,int y1)
{
int i;
priority_queue<node>q;
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
node beg,end;
beg.x=x1;
beg.y=y1;
beg.time=0;
beg.key=0;
beg.snack=0;
q.push(beg);
vis[beg.x][beg.y][beg.key][beg.snack]=1;
while(!q.empty())
{
beg=q.top();
q.pop();
if(s[beg.x][beg.y]=='T'&&beg.key==m)
{
ans=beg.time;
return 1;
}
for(i=0;i<4;i++)
{
end.x=beg.x+Move[i][0];
end.y=beg.y+Move[i][1];
if(judge(end.x,end.y))
{
if(s[end.x][end.y]>='a'&&s[end.x][end.y]<='z')//找到蛇
{
end.snack=beg.snack;
end.key=beg.key;
int num=s[end.x][end.y]-'a';
if(end.snack&(1<<num))//判断蛇是否被杀死
end.time=beg.time+1;
else
end.time=beg.time+2;
end.snack|=(1<<num);//将这条蛇加入已杀死的标记
}
else if(s[end.x][end.y]>='1'&&s[end.x][end.y]<='9')//找到钥匙
{
end.snack=beg.snack;
end.key=beg.key;
end.time=beg.time+1;
int num=s[end.x][end.y]-'0';
if(end.key+1==num)//判断是否找到了n之前的钥匙
end.key++;
}
else //路
{
end.key=beg.key;
end.snack=beg.snack;
end.time=beg.time+1;
}
if(!vis[end.x][end.y][end.key][end.snack]) //判断当前位置是否标记
{
vis[end.x][end.y][end.key][end.snack]=1;
q.push(end);
}
}
}
}
return 0;
}
int main()
{
int x1,y1,i,j,k;
while(scanf("%d%d",&n,&m),n|m)
{
ans=0;
for(i=0;i<n;i++)
scanf("%s",s[i]);
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(s[i][j]=='K')
{
x1=i;y1=j;
}
if(s[i][j]=='S')
{
s[i][j]='a'+k++;//将不同的蛇标记出来
}
}
}
if(bfs(x1,y1))
printf("%d\n",ans);
else printf("impossible\n");
}
return 0;
}

  

hdu 5025 Saving Tang Monk(bfs+状态压缩)的更多相关文章

  1. HDU 5025 Saving Tang Monk 【状态压缩BFS】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Time Limit: 2000/1000 MS (Java/O ...

  2. [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

    Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. HDU 5025 Saving Tang Monk --BFS

    题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐 ...

  4. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  5. ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

    Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...

  6. HDU 5025 Saving Tang Monk

    Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...

  7. 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)

    /* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k ...

  8. HDU 5025 Saving Tang Monk(状态转移, 广搜)

    #include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...

  9. hdu.1044.Collect More Jewels(bfs + 状态压缩)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

随机推荐

  1. Asp.Net保存session的三种方法

    C#中保存Session的三种方法及Web.Config设置 1.保存session到sql server,需要指定Sql Server服务器,这种方法因为要读写数据库最慢 <sessionSt ...

  2. webservices上传文件

    客户端: ob_clean(); ob_start(); readfile("D:/44.jpg"); $logo = ob_get_clean(); $pararmArr = a ...

  3. Java类的执行顺序

    在Java中一个类包括:构造块.构造方法.静态块.main方法.普通方法. 通过下面的例子将看出一个类中各种方法的执行顺序: /** * @author zhengbinMac */ public c ...

  4. 一天一点MySQL复习——存储过程

    一.存储过程概念 使用SQL编写访问数据库的代码时,可用两种方法存储和执行这些代码,一种是在客户端存储代码,并创建向数据库服务器发送的SQL命令(或SQL语句),比如在C#.Java等客户端编程语言中 ...

  5. Android Traceroute 功能实现

    经常在windows下开发网络功能的人 经常会使用的命令就是tracert .而实际上 在app开发中,我们也经常要碰到类似的情况.比如你的app 出现了问题,你总不能让用户想办法 去tracert吧 ...

  6. 使用 foreach 操作数组

    foreach 并不是 Java 中的关键字,是 for 语句的特殊简化版本,在遍历数组.集合时, foreach 更简单便捷.从英文字面意思理解 foreach 也就是“ for 每一个”的意思,那 ...

  7. Android无法调用JS的问题解决

    1.启用JS webView.getSettings().setJavaScriptEnabled(true); 2.需要使用WebChromeClient(如果没有这个Client,很多东西不会响应 ...

  8. hdu 3572 Task Schedule(最大流)2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC

    题意: 告诉我们有m个任务和k个机器.第i个任务需要ci天完成,最早从第ai天开始,最晚在第bi天结束.每台机器每天可以执行一个任务.问,是否可以将所有的任务都按时完成? 输入: 首行输入一个整数t, ...

  9. ANT 发布项目中 build.xml 文件的详细配置

    xml 代码 <?xml version="1.0" encoding="UTF-8"?> <!-- name:对应工程名字 default: ...

  10. 开发环境配置(netbeans+ant迁移到eclipse+maven)

    新公司入职,接手一个离职人员的项目,拿到的源码是以一个压缩包,用netbeans开发,ant管理:前端:jsp+extjs,后端:springmvc+hibernate+activiti+spring ...