ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)
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)的更多相关文章
- [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)
Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- 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 ...
- hdu 5025 Saving Tang Monk(bfs+状态压缩)
Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Nove ...
- HDU 5025 Saving Tang Monk
Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...
- 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)
/* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k ...
- HDU 5025 Saving Tang Monk --BFS
题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐 ...
- HDU 5025 Saving Tang Monk(状态转移, 广搜)
#include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
随机推荐
- LCD驱动程序(二)
上节我们主要是对fb_info结构体的配置,对fb_info结构体的配置主要分为一下步骤: static int lcd_init(void){ /* 1. 分配一个fb_info */ s3c_lc ...
- dubbo开发中使用到的一些服务配置方式
通过之前的学习了解了dubbo的常规的使用,下面我们看看特殊情况或者说真实环境下使用dubbo的一些配置实例. 一.一个接口有多个实现时可以使用group来区分 1.服务提供者配置 <?xml ...
- Feign-独立使用-实战
目录 写在前面 1.1.1. 短连接API的接口准备 1.1.2. 申明远程接口的本地代理 1.1.3. 远程API的本地调用 写在最后 疯狂创客圈 亿级流量 高并发IM 学习实战 疯狂创客圈 Jav ...
- php header, 允许ajax跨域访问
<?php header('content-type:application:json;charset=utf8'); header('Access-Control-Allow-Origin:* ...
- 【python】-- 元组、字典
元组 元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表 用途:一般情况下用于自己写的程序能存下数据,但是又希望这些数据不会被改变,比如:数据库连接信息等 1.访问元 ...
- 2017-2018-1 20179209《Linux内核原理与分析》第十周作业
设备与模块 设备分类 块设备 块设备可以以块为单位寻址,块大小随设备不同而不同:设备通常支持重定位操作,也就是对数据的随机访问.块设备的例子有外存,光盘等. 字符设备 字符设备不可寻址,仅供数据的流式 ...
- ZOJ - 1505 Solitaire 【双向BFS】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1505 题意 一个8 * 8 的棋盘上面有四个棋子 棋子可以上下左 ...
- UVA - 10870 Recurrences 【矩阵快速幂】
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...
- DEV开发之界面皮肤
最终效果:正文本人的环境是 VS2013+DEV 13.21.第一步,新建项目,(忽略)???2.修改Form1.cs的基类,Form修改为DevExpress.XtraBars.Ribbon.Rib ...
- zabbix实现mysql数据库的监控(四)
前面介绍的内容都是用第三方开发好的插件进行mysql监控的,可能有些我们关心的监控内容并不在其中,这时一种常用的方法就是定义我们自己的脚本并将它整合到zabbix中,从而在原有监控的基础上进行有力的补 ...