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. 让WebRTC支持H264编解码

    近期实验了下怎样让WebRTC支持H264编码.记录下,供有须要的人參考. 说明一下,我是在 Ubuntu Server 14.04 下编译的 WebRTC ,使用 native(C++) api 开 ...

  2. Windows系统SVN服务器搭建与使用

    下载svn:https://tortoisesvn.net/downloads.zh.html下载svn服务器:https://www.visualsvn.com/server/download/(如 ...

  3. spring AOP理解和相关术语

    一.AOP理解 AOP:横向抽取机制,底层使用代理方式实现. 示例: 现有LogDAO接口以及实现Log接口的Log类.类有add的方法,现在要打印add方法的开始时间和结束时间.(即增强Log的ad ...

  4. mysql设置指定ip远程访问连接的方法

    本文实例讲述了mysql设置指定ip远程访问连接的方法,分享给大家供大家参考.具体实现方法如下: 1. 授权用户root使用密码jb51从任意主机连接到mysql服务器: 复制代码 代码如下: GRA ...

  5. 2014阿里实习生面试题——MySQL如何实现索引的

    这是2014阿里实习生北京站二面的一道试题: 在MySQL中,索引属于存储引擎级别的概念,不同存储引擎对索引的实现方式是不同的,比如MyISAM和InnoDB存储引擎. MyISAM索引实现: MyI ...

  6. 装箱问题【STL】

    7-9 装箱问题(20 分) 假设有N项物品,大小分别为s​1​​.s​2​​.-.s​i​​.-.s​N​​,其中s​i​​为满足1≤s​i​​≤100的整数.要把这些物品装入到容量为100的一批箱 ...

  7. 【Flask】query可用参数

    ### query可用参数:1. 模型对象.指定查找这个模型中所有的对象.2. 模型中的属性.可以指定只查找某个模型的其中几个属性.3. 聚合函数. * func.count:统计行的数量. * fu ...

  8. Linux上网设置

    ifconfig 命令查看网络设置 步骤1.配置/etc/sysconfig/network-scripts/ifcfg-eth0 里的文件.有的是(ifcfg-ens33) 文件 CentOS下的i ...

  9. 好久没更了,确实太忙了--dedecms篇

    最近写了一个地方新闻网站,可以看看:www.qiluhuabao.com.模仿www.bashan.com写的 用的是dedecms,只听过,没用过.终于在上周来了一个必须用到cms的项目,现学现卖, ...

  10. Oracle的权限管理

    --授予系统权限语法 grant system_privilege | all privileges --可以设置select any dictionary权限之外的索引系统权限 to {user i ...