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
 
题意
孙悟空要去救师傅,K代表起点,T为唐僧位置,S代表有蛇的房间(不会超过五条蛇),#为剧毒房间,无法进入 带数字的房间为有钥匙的房间(不超过9个),孙悟空必须先拿到第一个钥匙,再拿第二个....最后拿到第m个钥匙,才能解救唐僧 没有拿到第m个钥匙的时候可以路过但是不能解救唐僧,路过有蛇的房间的时候需要花一分钟杀蛇,杀掉之后再经过时就不用再花时间杀蛇,但是每经过一个房间需要花费一分钟,求就出唐僧最短的时间。
 
分析
由于杀蛇需要花费时间,所以不能是找到了唐僧就是最短时间,必须将所有的可能性取最小值,这是这道题和不同的bfs最大的不同,另外,需要一个五位二进制数来存目前杀蛇的情况(这样一个数就可以概括全部的五条蛇,当然我认为也可以用一个数组来存,但是太费空间)
 
下面是代码,直接看代码更容易理解
  #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define N 105
#define INF 0x3f3f3f3f struct node
{
int x, y, t, key, snake;
node() {}
node(int x, int y, int t, int key, int snake) : x(x), y(y), t(t), key(key), snake(snake) {}
};
bool vis[N][N][][];
int n, m, sx, sy, ex, ey;
int dx[] = {, -, , }, dy[] = {, , , -};
char maze[N][N]; bool check(int x, int y)
{
if(<=x&&x<n&&<=y&&y<n&&maze[x][y]!='#') return true; //判断能不能走
return false;
} void bfs()
{
int ans = INF;
memset(vis, , sizeof(vis));
queue<node> que;
while(!que.empty()) que.pop(); //清空队列 因为有多个测试
que.push(node(sx, sy, , , ));
while(!que.empty()) {
node top = que.front(); que.pop();
int x = top.x, y = top.y, key = top.key, snake = top.snake, t = top.t;
if(key == m && maze[x][y] == 'T') {
ans = min(ans, t); //取最短时间
}
if(vis[x][y][key][snake] != ) continue; //判断当前“状态”有没有访问过,注意是“状态”,不是房间,房间是可以重复访问的
vis[x][y][key][snake] = ;
for(int i = ; i < ; i++) {
int nx = x + dx[i], ny = y + dy[i];
if(!check(nx, ny)) continue;
node now = top;
if('A' <= maze[nx][ny] && maze[nx][ny] <= 'G') {
//只有五条蛇,不能写 <= 'Z'
int s = maze[nx][ny] - 'A';
if((<<s) & now.snake) ; //如果蛇被打了
else {
now.snake |= (<<s); //没被打,时间加1 记录打蛇情况
now.t++;
}
} else if(maze[nx][ny] - '' == now
.key + ) {
now.key++;
}
now.t++;
que.push(node(nx, ny, now.t, now.key, now.snake));
}
}
if(ans != INF) printf("%d\n", ans);
else printf("impossible\n");
} int main()
{
while(~scanf("%d%d", &n, &m), n+m) {
int cnt = ;
for(int i = ; i < n; i++) {
scanf("%s", maze[i]);
}
for(int i = ; i < n; i++) {
for(int j = ; j < n; j++) {
if(maze[i][j] == 'K') sx = i, sy = j;
if(maze[i][j] == 'S') {maze[i][j] = cnt+'A'; cnt++;}
}
}
bfs();
}
return ;
}

HDU 5025 Saving Tang Monk的更多相关文章

  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 【状态压缩BFS】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Time Limit: 2000/1000 MS (Java/O ...

  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. 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. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

随机推荐

  1. 吴恩达课后作业学习1-week4-homework-multi-hidden-layer -2

    参考:https://blog.csdn.net/u013733326/article/details/79767169 希望大家直接到上面的网址去查看代码,下面是本人的笔记 实现多层神经网络 1.准 ...

  2. Python脱产8期 Day06 2019/4/18

    一 深浅拷贝 例:ls = [1, 'abc', [10]] 1.值拷贝:s1 = ls    # ls1直接将ls中存放的地址拿过来,>ls内部的值发生任何变化,ls1都会随之变化. 2.浅拷 ...

  3. Ubuntu18.04安装英伟达显卡驱动

    前几天买了一张RTX2060显卡,想自学一下人工智能,跑一些图形计算,安装Ubuntu18.04后发现英伟达显卡驱动安装还是有点小麻烦,所以这里记录一下安装过程,以供参考: 1.卸载系统里低版本的英伟 ...

  4. CF434D Nanami's Power Plant 最小割

    传送门 因为连距离限制的边的细节调了贼久QAQ 这个题和HNOI2013 切糕性质相同,都是有距离限制的最小割问题 对于每一个函数,用一条链记录变量\(x\)在不同取值下这个函数的贡献.对于一个\(x ...

  5. Volley使用

    Volley是常用的网络请求框架,主要的用法如下: 获取字符串: public static void volleyTest1(final Context context){ RequestQueue ...

  6. DOM(二)

    文档信息 document对象还有一些标准的Document对象所没有的属性: title属性:包含着<title>元素中的文本——显示在浏览器窗口的标题栏或标签页上,通过整个属性可以取得 ...

  7. Ansible 简介

    Ansible 是一个开源的基于 OpenSSH 的自动化配置管理工具.可以用它来配置系统.部署软件和编排更高级的 IT 任务,比如持续部署或零停机更新.Ansible 的主要目标是简单和易用,并且它 ...

  8. 深入理解Redis Cluster

    Redis Cluster采用虚拟槽分区,所有的key根据哈希函数映射到0~16383槽内,计算公式: slot = CRC16(key) & 16383 每个节点负责维护一部分槽以及槽所映射 ...

  9. 朱晔和你聊Spring系列S1E1:聊聊Spring家族的几大件

    朱晔和你聊Spring系列S1E1:聊聊Spring家族的几大件 [下载本文PDF进行阅读] Spring家族很庞大,从最早先出现的服务于企业级程序开发的Core.安全方面的Security.到后来的 ...

  10. python第三章:循环语句--小白博客

    Python条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非 ...