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

这个题目,读完题目第一反应就是搜索。但是这个题目有几个注意点。读入需要当心,此外,此处用bfs的 话,如果搜到就return,不一定是最小的,因为走过S的第一次时候会使时间负担增加,故最好使用优先队列。此外不止一个S,而且每个S只起一次作用, 也比较棘手,最重要的是要按顺序找到钥匙才行,使得bfs的状态变得很复杂。此处使用key变量表示当前开了几把锁,而S则人为进行排序用v数组才保存访 问状态。由于比赛时写的代码,所以,对S的处理有点浪费内存。

代码:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define inf 0x3fffffff
#define esp 1e-10
using namespace std;
struct node
{
int x, y, time, key;
bool v[];
bool operator < (const node &a) const
{
return time > a.time;
}
};
char Map[][];
int visit[][][], n, m;
int sn[][], now;
int bfs (int ix, int iy)
{
node f;
f.x = ix; f.y = iy;
f.time = ;
f.key = ;
memset (f.v, , sizeof (f.v));
priority_queue < node > q;
q.push(f);
while (!q.empty())
{
node k = q.top();
q.pop();
if (Map[k.x][k.y] == 'T' && k.key == m)
{
return k.time;
}
for (int xx = -; xx <= ; ++xx)
{
for (int yy = -; yy <= ; ++yy)
{
if (xx != && yy != )
continue;
if (xx == && yy == )
continue;
if (k.x + xx < || k.x + xx >= n)
continue;
if (k.y + yy < || k.y + yy >= n)
continue;
if (Map[k.x + xx][k.y + yy] == '#')
continue;
int dt = ;
if (Map[k.x + xx][k.y + yy] == 'S' && k.v[sn[k.x + xx][k.y + yy]] == )
dt = ;
int kk = k.key;
if (Map[k.x + xx][k.y + yy] - '' == k.key + )
kk = k.key + ;
if (visit[k.x+xx][k.y+yy][kk] == || visit[k.x+xx][k.y+yy][kk] > k.time+dt)
{
visit[k.x + xx][k.y + yy][kk] = k.time + dt;
f = k;
f.x = k.x + xx; f.y = k.y + yy;
f.time = k.time + dt;
f.key = kk;
if (Map[k.x + xx][k.y + yy] == 'S')
{
f.v[sn[k.x + xx][k.y + yy]] = ;
}
q.push(f);
}
}
}
}
return -;
}
int main()
{
freopen ("test.txt", "r", stdin);
while (scanf ("%d%d", &n, &m) != EOF && (m+n) != )
{
now = ;
memset (sn, -, sizeof(sn));
getchar();
int ix, iy;
for (int i = ; i < n; ++i)
{
for (int j = ; j < n; ++j)
{
Map[i][j] = getchar();
if (Map[i][j] == 'K')
{
ix = i;
iy = j;
}
if (Map[i][j] == 'S')
{
sn[i][j] = now++;
}
}
getchar();
}
memset (visit, , sizeof (visit));
int ans = bfs(ix, iy);
if (ans != -)
printf ("%d\n", ans);
else
printf ("impossible\n");
}
return ;
}

ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)的更多相关文章

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

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

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

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

  3. 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 ...

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

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

  5. HDU 5025 Saving Tang Monk

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

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

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

  7. HDU 5025 Saving Tang Monk --BFS

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

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

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

  9. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

随机推荐

  1. poj3708(公式化简+大数进制装换+线性同余方程组)

    刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...

  2. Ajax之基础总结

    一.Ajax 简介 Ajax 由 HTML.JavaScript技术.DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序.在详细探讨 Ajax 是 ...

  3. easyUI参数传递Long型时,前台解析出错的问题——SKY

    果发现datagrid在显示Long类型数据时有问题.问题如下:比如一个数据ID为20121229101239002,经过转换之后的JSON数据也没有问题,但是在显示的时候就会显示为201212291 ...

  4. shu7-19【背包和母函数练习】

    题目:http://acm.hdu.edu.cn/diy/contest_show.php?cid=20083 密码:shuacm 感觉他们学校的新生训练出的比较好. 今天很多题目都是强化了背包的转化 ...

  5. 2.设计模式---Adapter模式

    Adapter模式也就是适配器模式,最常见的就是这个:码农必备-------------->笔记本电源适配器: 那么这玩意到底是干嘛的?? 手工画图一张: 220V--------------- ...

  6. Netty环境搭建 (源码死磕2)

    [正文]netty源码  死磕2: 环境搭建 本小节目录 1. Netty为什么火得屌炸天? 1.1. Netty是什么? 1.2. Netty火到什么程度呢? 1.3. Netty为什么这么火? 2 ...

  7. [luogu3359]改造异或树

    [luogu3359]改造异或树 luogu 和之前某道题类似只有删边的话考虑倒着加边 但是怎么统计答案呢? 我们考虑以任意点为根dfs一遍求出每个点到根的路径异或和s[i] 这样任意两点x,y的路径 ...

  8. docker centos yum 源

    aliyun yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.rep ...

  9. java面试之数据库面试知识点【转】

    1. 主键 超键 候选键 外键 主 键: 数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合.一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null). 超 键: 在关系中 ...

  10. Property Animator基本用法

    ObjectAnimator anim=ObjectAnimator.ofFloat(textview, "alpha", 0f, 1f); //ObjectAnimator an ...