任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025

Saving Tang Monk

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

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
 
Source

题意概括:

给一个 N * N 的地图,孙悟空起点在 K ,唐僧起点在 T,数字 i 代表第 i 把钥匙,# 是毒气区不能进入, S 有蛇(需要杀死才能通过,只需要杀死一次);

孙悟空要按顺序集齐 M 把钥匙才能解救师父,求最短的时间,如果没有输出“impossible”。

解题思路:

BFS找最短路,因为蛇最多只有5条,所以状态压缩判断哪条已杀,哪条未杀;如果未杀则当前需要多走一步,反则不用。

集钥匙只需要一个变量 cnt_key 记录已经集了多少把钥匙,那么接下俩可以拿的钥匙就是 第 cnt_key+1 把。

BFS需要一个 vis[ x ][ y ][ key ][ snake ] 来book一下状态,已经处理过的不再处理。

最后一个debug了很久的原因:多测试用例当 N M 都为 0 时( ... &&(N+M))才结束,习惯性打了( ... &&N && M),凉凉。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = ;
int nx[] = {-, , , };
int ny[] = {, , -, };
struct date
{
int x, y, t, key, snake;
date(int _x = , int _y = , int _t = , int _key = , int _snake = ):x(_x), y(_y), t(_t), key(_key), snake(_snake){}
}; char mmp[MAXN][MAXN];
bool vis[MAXN][MAXN][][];
int N, M, cnt;
int sx, sy, ex, ey; void solve()
{
memset(vis, false, sizeof(vis));
int ans = INF; //初始化步数
queue<date> sq;
sq.push(date(sx, sy, , , ));
//vis[sx][sy] = true;
date tp;
while(!sq.empty()){
tp = sq.front(), sq.pop();
int x = tp.x, y = tp.y, key = tp.key, snake = tp.snake, t = tp.t;
if(key == M && mmp[x][y] == 'T') ans = min(ans, t); //集齐钥匙并且到达终点
if(vis[x][y][key][snake]) continue; //状态已访问过
vis[x][y][key][snake] = true; //状态不重复
for(int k = ; k < ; k++){
int tx = x + nx[k], ty = y + ny[k];
if(tx < || ty < || tx == N || ty == N || mmp[tx][ty] == '#') continue; date now = tp; if('A' <= mmp[tx][ty] && mmp[tx][ty] <= 'G'){
int s = mmp[tx][ty] - 'A';
if((<<s) & now.snake); //蛇被被打了
else{ //蛇没有被打
now.snake |= (<<s);
now.t++;
}
}
else if(mmp[tx][ty] - '' == now.key + ){ //捡钥匙,遇到当前可以捡的钥匙
now.key++;
}
now.t++;
sq.push(date(tx, ty, now.t, now.key, now.snake));
}
}
if(ans >= INF) puts("impossible");
else printf("%d\n", ans);
} int main()
{
while(~scanf("%d%d", &N, &M) && (N+M)){
cnt = ;
for(int i = ; i < N; i++){
scanf("%s", &mmp[i]);
for(int j = ; j < N; j++){
if(mmp[i][j] == 'K') sx = i, sy = j;
if(mmp[i][j] == 'S') mmp[i][j] = cnt+'A', cnt++;
}
}
solve();
}
return ;
}

HDU 5025 Saving Tang Monk 【状态压缩BFS】的更多相关文章

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

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

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

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

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

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

  4. hdu 5025 Saving Tang Monk(bfs+状态压缩)

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

  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 --BFS

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

  9. ACM-ICPC2018北京网络赛 Saving Tang Monk II(bfs+优先队列)

    题目1 : Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also < ...

随机推荐

  1. keepalived heartbeat lvs haproxy

    一, keeplived @ 01,keeplived 是什么? Keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态,它根据TCP/IP参考模型的第三.第四层.第五层交 ...

  2. Tortoise SVN安装后右键没有菜单的解决方法

    最近换了WIN7的操作系统,感觉用起来爽极了,于是开始一个个装软件,最让我头疼的就是安装SVN,安装没有问题,完成后却无法显示右键菜单,开始同事也帮我找原因,以为是我设置问题或者哪里阻止了程序,还以为 ...

  3. TOJ 1690 Cow Sorting (置换群)

    Description Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow ...

  4. 02.ArrayList和HashTable

    ArrayList集合 数组的缺点: (1).数组只能存储相同类型的数据. (2).数组的长度要在定义时确定. 集合的好处: (1).集合可以存储多种不同类型的数据. (2).集合的长度是可以任意改变 ...

  5. 转载:.NET Memory Leak: XmlSerializing your way to a Memory Leak

    原文地址:http://blogs.msdn.com/b/tess/archive/2006/02/15/532804.aspx I hate to give away the resolution ...

  6. 写C#代码时用到的中文简体字 、繁体字 对应的转化 (收藏吧)

    简体字    下面有与之对应的繁体字 private const String Jian = "啊阿埃挨哎唉哀皑癌蔼矮艾碍爱隘鞍氨安俺按暗岸胺案肮昂盎凹敖熬翱袄傲奥懊澳芭捌扒叭吧笆疤巴拔跋靶 ...

  7. OLEDB 数据变更通知

    除了之前介绍的接口,OLEDB还定义了其他一些支持回调的接口,可以异步操作OLEDB对象或者得到一些重要的事件通知,从而使应用程序有机会进行一些必要的处理.其中较有用的就是结果集对象的变更通知接口.通 ...

  8. 使用Anaconda管理环境

    Anaconda指的是一个开源的python发行版本,其包含了conda.Python等180多个科学包及其依赖项. Anaconda是一个开源的包.环境管理器,可以用于在同一个机器上安装不同版本的软 ...

  9. Html5的map在实际使用中遇到的问题及解决方案

    前言:百度了一下html map,嗯嗯,介绍的挺详细的,如果是初学者,直接看他们的教程,挺好的,就不用我再多说了. 不过我发现一个问题,就是都是介绍map有什么属性怎么用的,这明显就是照搬文档自己再改 ...

  10. arcgis 10.2 安装教程(含下载地址)

    http://jingyan.baidu.com/article/fc07f98911b66912ffe5199b.html 2013年7月,Esri即将推出全新的版本——ArcGIS 10.2,那些 ...