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. Pycharm 中错误ImportError: No module named appium

    Q: Pycharm 中错误ImportError: No module named appium A: Pycharm IDE Preferences -> Project Interpret ...

  2. 【BZOJ4154】[Ipsc2015]Generating Synergy KDtree

    [BZOJ4154][Ipsc2015]Generating Synergy Description 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问 ...

  3. jqcloud 标签云效果

    官网地址: http://mistic100.github.io/jQCloud/index.htmlgithub 地址: https://github.com/lucaong/jQCloud使用 & ...

  4. SpringMVC拦截器实现登录认证

    项目结构如图: 需要的jar:有springMVC配置需要的jar和jstl需要的jar SpringMVC包的作用说明: aopalliance.jar:这个包是AOP联盟的API包,里面包含了针对 ...

  5. Python菜鸟之路:前端HTML基础

    前面的章节中,Python的基本知识已经差不多介绍完了.本节介绍HTML相关的知识.需要着重声明的是,前端知识是非常非常重要的知识,以我实际项目经验来看,一个项目的瓶颈在设计和前端.设计就先不说了,前 ...

  6. Java基础 - 常量与变量

    A:常量 内存中的一小块区域,在程序执行过程中,其值不可以发生改变的量称为常量 常量的几种表现形式: a:字符串常量 "HelloWorld" b:整数常量 12 c:小数常量 1 ...

  7. CSS图片居中,多余隐藏

    /*外层DIV*/ div {position: relative;overflow:hidden;width: 显示宽度px;} /*left=50%刚好在中间,margin-left=往前移动图片 ...

  8. Cordova-安装Cordova过程详细解

    官方网站Apache Cordova 前提是你电脑上 1:全局安装了Node 2:全局安装了npm 3:安装了java,并配置好环境 4:下载安装好android-sdk,并配好环境,注意安卓虚拟机可 ...

  9. xpath中如何使用变量

    xpath (python)xpath中如何使用变量描述: 在xpath中该如何使用变量,想选择id是某个值的元素,这个值是个变量. response.xpath('//div[@id=val]'). ...

  10. shell 日期加减运算

    比如今日是2012-04-22 $ date -d "+1 day" +%Y-%m-%d 2012-04-23   $ date -d "-1 day" +%Y ...