《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.

InputThere 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.OutputFor 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

用flag[x][y][key][st] 的四维下标记录 是否在最多得到key钥匙,打败过st(状态压缩)蛇的状态下 到达过坐标(x,y)的位置。

本题与相较于最基础的迷宫问题一是增加了钥匙这一物品,对此只需要通过记录每个状态时得到的钥匙的最大编号即可处理。另一个不同之处就是引入了蛇,并且还存在重新走回原本有蛇的位置的可能。这就需要我们通过状态压缩记录每一个状态时蛇是否被消灭了。为此将蛇预先编上号,状压即可。(总共最多5条,只有32种状态)

  1 #include <iostream>
  2 #include <string>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <queue>
  8 #include <set>
  9 #include <map>
 10 #include <list>
 11 #include <vector>
 12 #include <stack>
 13 #define mp make_pair
 14 //#define P make_pair
 15 #define MIN(a,b) (a>b?b:a)
 16 //#define MAX(a,b) (a>b?a:b)
 17 typedef long long ll;
 18 typedef unsigned long long ull;
 19 const int MAX=1e2+5;
 20 const int INF=1e8+5;
 21 using namespace std;
 22 //const int MOD=1e9+7;
 23 typedef pair<ll,int> pii;
 24 const double eps=0.00000001;
 25 int n,m;
 26 int sx,sy,ex,ey;//起点终点的坐标
 27 int dx[4]={0,0,1,-1};
 28 int dy[4]={1,-1,0,0};
 29 char a[MAX][MAX];
 30 bool vi[MAX][MAX][10][35];
 31 map <pii,int> id;//守卫的编号
 32 bool check(int x,int y)
 33 {
 34     return x>=1&&x<=n&&y>=1&&y<=n&&a[x][y]!='#';
 35 }
 36 struct status
 37 {
 38     short x,y;
 39     short keys;
 40     short fighted;
 41     int steps;
 42     status(){}
 43     status(short p1,short p2,short p3,short p4,int p5){x=p1,y=p2,keys=p3,fighted=p4,steps=p5;}
 44 };
 45 void pre()
 46 {
 47     int cnt=0;
 48     for(int i=1;i<=n;i++)
 49         for(int j=1;j<=n;j++)
 50         {
 51             if(a[i][j]=='K')
 52             {
 53                 sx=i,sy=j;
 54             }
 55             else if (a[i][j]=='T')
 56             {
 57                 ex=i,ey=j;
 58             }
 59             else if(a[i][j]=='S')
 60             {
 61                 id[mp(i,j)]=cnt++;
 62             }
 63         }
 64 }
 65 int bfs()
 66 {
 67     status tem;
 68     tem.x=sx,tem.y=sy,tem.keys=0,tem.fighted=0,tem.steps=0;
 69     queue<status>que;
 70     int an=INF;
 71     que.push(tem);
 72     vi[sx][sy][0][0]=true;
 73     while(!que.empty())
 74     {
 75         tem=que.front();
 76         int nowx=tem.x,nowy=tem.y;
 77         if(nowx==ex&&nowy==ey&&tem.keys==m)
 78             an=min(an,tem.steps);
 79         que.pop();
 80         for(int i=0;i<4;i++)
 81         {
 82             int x1=nowx+dx[i];
 83             int y1=nowy+dy[i];
 84             if(check(x1,y1))
 85             {
 86                 if(a[nowx][nowy]!='S'||((1<<id[mp(nowx,nowy)])&tem.fighted))//非在蛇的位置 或在已经消灭的蛇那里
 87                 {
 88                     if(a[x1][y1]>='1'&&a[x1][y1]<='9')
 89                     {
 90                         int num=a[x1][y1]-'0';
 91                         if(tem.keys+1==num)//如果是可以得到的新钥匙
 92                         {
 93                             if(!vi[x1][y1][num][tem.fighted])
 94                             {
 95                                 vi[x1][y1][num][tem.fighted]=true;
 96                                 que.push(status(x1,y1,num,tem.fighted,tem.steps+1));
 97                             }
 98                         }
 99                         else
100                         {
101                             if(!vi[x1][y1][tem.keys][tem.fighted])
102                             {
103                                 vi[x1][y1][tem.keys][tem.fighted]=true;
104                                 que.push(status(x1,y1,tem.keys,tem.fighted,tem.steps+1));
105                             }
106                         }
107                     }
108                     else//普通的房间
109                     {
110                         if(!vi[x1][y1][tem.keys][tem.fighted])
111                         {
112                             vi[x1][y1][tem.keys][tem.fighted]=true;
113                             que.push(status(x1,y1,tem.keys,tem.fighted,tem.steps+1));
114                         }
115                     }
116                 }
117                 else//当前在有蛇 且没有与其打过的房间
118                 {
119                     if(a[x1][y1]>='1'&&a[x1][y1]<='9')
120                     {
121                         int num=a[x1][y1]-'0';
122                         if(tem.keys+1==num)//如果是可以得到的新钥匙
123                         {
124                             if(!vi[x1][y1][num][(tem.fighted|(1<<id[mp(nowx,nowy)]))])
125                             {
126                                 vi[x1][y1][num][(tem.fighted|(1<<id[mp(nowx,nowy)]))]=true;
127                                 que.push(status(x1,y1,num,(tem.fighted|(1<<id[mp(nowx,nowy)])),tem.steps+2));
128                             }
129                         }
130                         else
131                         {
132                             if(!vi[x1][y1][tem.keys][(tem.fighted|(1<<id[mp(nowx,nowy)]))])
133                             {
134                                 vi[x1][y1][tem.keys][(tem.fighted|(1<<id[mp(nowx,nowy)]))]=true;
135                                 que.push(status(x1,y1,tem.keys,(tem.fighted|(1<<id[mp(nowx,nowy)])),tem.steps+2));
136                             }
137                         }
138                     }
139                     else//普通的房间
140                     {
141                         if(!vi[x1][y1][tem.keys][(tem.fighted|(1<<id[mp(nowx,nowy)]))])
142                         {
143                             vi[x1][y1][tem.keys][(tem.fighted|(1<<id[mp(nowx,nowy)]))]=true;
144                             que.push(status(x1,y1,tem.keys,(tem.fighted|(1<<id[mp(nowx,nowy)])),tem.steps+2));
145                         }
146                     }
147                 }
148             }
149         }
150     }
151     return an;
152 }
153 int main()
154 {
155     while(scanf("%d%d",&n,&m)&&n)
156     {
157         memset(vi,false,sizeof(vi));
158         id.clear();
159         for(int i=1;i<=n;i++)
160             scanf("%s",a[i]+1);
161         pre();
162         int an=bfs();
163         if(an==INF)
164             printf("impossible\n");
165         else
166             printf("%d\n",an);
167     }
168     return 0;
169 }

转载于:https://www.cnblogs.com/quintessence/p/7213969.html

(BFS/状态压缩)HDU 5025 Saving Tang Monk相关推荐

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

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

  4. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  5. csu 1536 Bit String Reordering(模拟 bfs+状态压缩)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1536 题意: 输入n个只为 0或1 的数 形成一个排列 再输入m个数 每个数代表 目标排列 (样例 ...

  6. BFS + 状态压缩总结

    BFS + 状态压缩使用条件 求最短路径时,一般来说会优先考虑使用BFS算法.BFS算法在广度优先搜索的过程中会有一个类似vis的数组去重,避免重复访问 但是在一些情况下,题目需要求最短路径的同时,有 ...

  7. nyist 999 师傅又被妖怪抓走了 【双广搜 || BFS +状态压缩】

    题目:nyist 999 师傅又被妖怪抓走了 分析:在一个图中只要看到D点和E点就行的最小步数,看到的定义是:也就是说两个人在同一行或者同一列,并且中间没有障碍物或者没有其他人就可以看到对方. 所以可 ...

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

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

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

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

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

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

最新文章

  1. LabVIEW做一款科学计算器
  2. 监控软件nagios之配置文件详解
  3. 美国互联网广告07年总开支255亿美元, 增长27%
  4. 一次内网靶场学习记录
  5. 减少图片HTTP 请求的方案
  6. c++电费管理系统_能耗管理系统
  7. LeetCode 1065. 字符串的索引对
  8. 这款能够生成文档的接口测试软件,为什么越来越受欢迎?
  9. python数字排序_python按照列表元素中的数字大小排序
  10. Maven项目jar包依赖冲突的原因以及解决办法
  11. python 绘制一维散点图
  12. Excel-DATEDIF函数计算两日期天数差
  13. 利用浏览器制作一款包含3D效果的演示文案
  14. Kata Containers及相关vmm介绍
  15. Python Pandas库 Series.dt.tz_localize()和 Series.dt.tz_convert()的简单使用
  16. 如何计算每个月有多少天
  17. OpenShift 4 - 在单机版 OpenShift Local 中运行 OpenShift Virtualization(视频)
  18. Vue3 究竟好在哪里?(和 React Hook 的详细对比)
  19. 洛谷P1851 好朋友
  20. MySQL约束条件和多表查询方式详解

热门文章

  1. java 正则表达式match_详解正则表达式匹配方法 match()
  2. Python调整图片的文件大小
  3. Byte,Kb,Mb,Gb,Tb,Pb,Eb,Zb,Yb,Bb单位换算
  4. the JDBC Driver has been forcibly unregistered问题解决
  5. java 中文url转码_对 url 中含有的中文进行转码操作
  6. #Android-中控指纹仪Live R20 开发注意事项#
  7. Office办公软件测试题
  8. 蘑菇租房java,租房经历总结-----我是如何2天找到合适租房的(房东直租)简单粗暴...
  9. [SSM]报错500:org.springframework.dao.DataIntegrityViolationException
  10. 英语语法篇 - 动词的分类和形式