任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025

Saving Tang Monk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3242    Accepted Submission(s): 1127

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
Source
2014 ACM/ICPC Asia Regional Guangzhou Online

题意概括:

给一个 N * N 的地图,孙悟空起点在 K ,唐僧起点在 T,数字 i 代表第 i 把钥匙,# 是毒气区不能进入, S 有蛇(需要杀死才能通过,只需要杀死一次);

孙悟空要按顺序集齐 M 把钥匙才能解救师父,求最短的时间,如果没有输出“impossible”。

解题思路:

BFS找最短路,因为蛇最多只有5条,所以状态压缩判断哪条已杀,哪条未杀;如果未杀则当前需要多走一步,反则不用。

集钥匙只需要一个变量 cnt_key 记录已经集了多少把钥匙,那么接下俩可以拿的钥匙就是 第 cnt_key+1 把。

BFS需要一个 vis[ x ][ y ][ key ][ snake ] 来book一下状态,已经处理过的不再处理。

最后一个debug了很久的原因:多测试用例当 N M 都为 0 时( ... &&(N+M))才结束,习惯性打了( ... &&N && M),凉凉。

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <queue>
 6 #include <cmath>
 7 #define INF 0x3f3f3f3f
 8 using namespace std;
 9 const int MAXN = 105;
10 int nx[] = {-1, 1, 0, 0};
11 int ny[] = {0, 0, -1, 1};
12 struct date
13 {
14     int x, y, t, key, snake;
15     date(int _x = 0, int _y = 0, int _t = 0, int _key = 0, int _snake = 0):x(_x), y(_y), t(_t), key(_key), snake(_snake){}
16 };
17
18 char mmp[MAXN][MAXN];
19 bool vis[MAXN][MAXN][10][40];
20 int N, M, cnt;
21 int sx, sy, ex, ey;
22
23 void solve()
24 {
25     memset(vis, false, sizeof(vis));
26     int ans = INF;                                  //初始化步数
27     queue<date> sq;
28     sq.push(date(sx, sy, 0, 0, 0));
29     //vis[sx][sy] = true;
30     date tp;
31     while(!sq.empty()){
32         tp = sq.front(), sq.pop();
33         int x = tp.x, y = tp.y, key = tp.key, snake = tp.snake, t = tp.t;
34         if(key == M && mmp[x][y] == 'T') ans = min(ans, t);                     //集齐钥匙并且到达终点
35         if(vis[x][y][key][snake]) continue;    //状态已访问过
36         vis[x][y][key][snake] = true;               //状态不重复
37         for(int k = 0; k < 4; k++){
38             int tx = x + nx[k], ty = y + ny[k];
39             if(tx < 0 || ty < 0 || tx == N || ty == N || mmp[tx][ty] == '#') continue;
40
41             date now = tp;
42
43             if('A' <= mmp[tx][ty] && mmp[tx][ty] <= 'G'){
44                 int s = mmp[tx][ty] - 'A';
45                 if((1<<s) & now.snake);               //蛇被被打了
46                 else{                               //蛇没有被打
47                     now.snake |= (1<<s);
48                     now.t++;
49                 }
50             }
51             else if(mmp[tx][ty] - '0' == now.key + 1){    //捡钥匙,遇到当前可以捡的钥匙
52                 now.key++;
53             }
54             now.t++;
55             sq.push(date(tx, ty, now.t, now.key, now.snake));
56         }
57     }
58     if(ans >= INF) puts("impossible");
59     else printf("%d\n", ans);
60 }
61
62 int main()
63 {
64     while(~scanf("%d%d", &N, &M) && (N+M)){
65         cnt = 0;
66         for(int i = 0; i < N; i++){
67             scanf("%s", &mmp[i]);
68             for(int j = 0; j < N; j++){
69                 if(mmp[i][j] == 'K') sx = i, sy = j;
70                 if(mmp[i][j] == 'S') mmp[i][j] = cnt+'A', cnt++;
71             }
72         }
73         solve();
74     }
75     return 0;
76 }

View Code

HDU 5025 Saving Tang Monk 【状态压缩BFS】相关推荐

  1. HDU 5025 Saving Tang Monk(广州网络赛D题)

    HDU 5025 Saving Tang Monk 题目链接 思路:记忆化广搜,vis[x][y][k][s]表示在x, y结点,有k把钥匙了,蛇剩余状态为s的步数,先把图预处理出来,然后进行广搜即可 ...

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

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

  3. 【HDU - 5094】 Maze (状态压缩+bfs)

    题干: This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enterp ...

  4. HDU 5025:Saving Tang Monk(BFS + 状压)

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

  5. Saving Tang Monk II HihoCoder - 1828(2018北京网络赛三维标记+bfs)

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

  6. #hihoCoder #1828 : Saving Tang Monk II (分层BFS)

    描述 <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chi ...

  7. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A Saving Tang Monk II【分层bfs】

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also <Monkey>) is one of the ...

  8. hdu5094(上海邀请赛E) 状态压缩bfs:取钥匙开门到目的地

    貌似和前段时间的一场网络赛一个类型,状态压缩判断走没走过,然后裸bfs到终点. 其实这类题目很简单就是一个bfs模板,无非就是有一些坑,比如这一题..一个格子可以有多把钥匙汗== 1 #include ...

  9. hdu4845 状态压缩BFS

    题意:      给一个n*m的矩阵,从11,走到nm,格子和格子之间可能有墙,也可能有门,有的格子上面有钥匙,相应的钥匙开相应的们,捡钥匙和开门都不需要时间,问你最少多少部能走到nm. 思路:   ...

  10. HDU 1074 Doing Homework【状态压缩DP】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...

最新文章

  1. 一堆棋子java代码编程_网易2018校招内推编程题-堆棋子-C++实现
  2. 干货满满的 Python 实战项目,点赞收藏
  3. 脑与神经科学3 脑神经影像上
  4. 苹果手机java_iphone手机,苹果手机如何登陆网易163邮箱
  5. 快速融入新团队的一点个人体会
  6. ubuntu snmp Error: unknown payload OID
  7. 【工程化】从0搭建VueJS移动端组件库开发框架
  8. java+mysql性能优化_Java培训实战教程之mysql优化
  9. mysql group by 查询慢_MySQL慢查询优化(线上案例调优)
  10. Arduino入门笔记(6):温度传感器及感温杯实验
  11. apt-get 很有用的一个命令
  12. unity3d:路径点移动,使用dotween(模拟蝴蝶飞舞)
  13. 算法篇----求两数的最大公约数和最小公倍数
  14. CHD搭建的环境中,解决用户权限的问题
  15. strcpy与strncpy的实现
  16. nginx支持text html,BT面板重启Nginx提示“nginx: [warn] duplicate MIME type “text/html””解决办法...
  17. cat实时监控-入门demo
  18. FT1248程序(FT232H,FT220X)
  19. Vanish搭建CDN的节点集群
  20. 物理渲染数学(s2013_pbs_physics_math_notes)

热门文章

  1. python中pyecharts 柱状图 折线图混用_pyecharts折线图和柱状图
  2. html中改变一张图片的颜色,css怎么改变图片颜色
  3. Excel单元格保护
  4. No toolchains found in the NDK toolchains folder for ABI with prefix:XXX
  5. 大数据分析-实验五 pdfminer
  6. VisualStudio各版本的安装与使用(持续更新)
  7. 如何系统地自学 Python?
  8. 计算机标准差平方差怎么按,数学标准差公式
  9. 标准差公式中,分母是n还是n-1?
  10. Redis 单线程却能支撑高并发