[状态压缩 广搜BFS]Saving Tang Monk

2024-06-21 12:04
文章标签 bfs 压缩 状态 saving tang monk

本文主要是介绍[状态压缩 广搜BFS]Saving Tang Monk,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

描述

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

输入

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.

输出

For each test case, print the minimum time (in minute) Sun Wokong needed to save Tang Monk. If it's impossible for Sun Wokong to complete the mission, print "impossible".

样例输入

3 1
K.S
##1
1#T
3 1
K#T
.S#
1#.
3 2
K#T
.S.
21.
0 0

样例输出

5
impossible
8
解题分析

这道题可以算是广搜的天花板之一了,不仅需要状态压缩的策略,还需要使用一些形如priority_queue的数据结构来辅助,而且关于已访问过的标记,包括拿到的钥匙,杀死的蛇等等细节都很值得推敲。以及说,我们可以用一个数组来存储我们钥匙的状态,并且我们考虑的是其二进制的表达式。首先,正常地读入数据,注意,由于我们可能有很多组数组,所以每次的while循环的开始,我们应该重置我们需要重置的一些变量,比如说访问数组归0,蛇计数数组归0,答案ans的重置。接着读入整个迷宫,需要注意的是,我们需要孙悟空的初始位置,以及我们读入蛇的时候,把这条蛇的信息放入一个数组里面,并且标记它的位置。对于我们广搜时放入priority_queue的节点的话,我们希望它有我们的位置,杀死蛇的情况,钥匙情况以及我们的步数,位置坐标和步数是很基本的。注意,我们每次的广搜,只走一步!所以我们在一次的循环里不能直接更改我们当前读取的队列开头的元素的信息,所以另设变量去处理是一个很好的选择。一共三种情况,要么是钥匙,要么是蛇,要么就是唐僧,分情况,利用位操作去判断,去处理,用&运算取出某一位,用|运算去把某一位设置为1,诸如此类,这般这般。最后输出答案即可。

代码演示
#include <iostream>
#include <cmath>
//#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>
#include <queue>
#include <stack>
#include <cstdlib>
#define INF (1<<30)
using namespace std;int N,M;
char maze[105][105];
bitset<513> vis_keys[105][105];
bitset<33> vis_snakes[105][105];
int ans=INF;
int Kx,Ky,k;
int keys[]={0,1,3,7,15,31,63,127,255,511};
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};struct Node{int x,y,snakes,keys,step;bool operator<(const Node& o) const{return step>o.step;}
};struct snake{int x,y;
} snakes[5];
Node tmp;int bfs(){priority_queue<Node> q;q.push({Kx,Ky,0,0,0});while(q.size()){tmp=q.top();q.pop();for(int i=0;i<4;i++){int x0=tmp.x+dx[i];int y0=tmp.y+dy[i];if(x0<0 || y0<0 || x0>=N || y0>=N || maze[x0][y0]=='#') continue;if(vis_keys[x0][y0][tmp.keys]==1 && vis_snakes[x0][y0][tmp.snakes]==1) continue;int nsnakes=tmp.snakes,nkeys=tmp.keys,nstep=tmp.step+1;if(isdigit(maze[x0][y0])){int m=maze[x0][y0]-'0';if(m==1 || (nkeys>>(m-2)) & 1){nkeys |= (1<<(m-1));}}else if(maze[x0][y0]=='S'){int index=0;for(int i=0;i<k;i++){if(snakes[i].x==x0 && snakes[i].y==y0){index=i;break;}}if(!((nsnakes>>index) & 1)){nsnakes |=(1<<index);nstep +=1;}}else if(maze[x0][y0]=='T' && nkeys==keys[M]){return nstep;}q.push({x0,y0,nsnakes,nkeys,nstep});vis_keys[x0][y0][nkeys]=1;vis_snakes[x0][y0][nsnakes]=1;}}return -1;
}int main(){while(scanf("%d%d",&N,&M)!=EOF){if(N==0 && M==0) break;memset(vis_keys,0,sizeof(vis_keys));memset(vis_snakes,0,sizeof(vis_snakes));k=0;ans=1<<30;for(int i=0;i<N;i++)for(int j=0;j<N;j++){scanf(" %c",&maze[i][j]);if(maze[i][j]=='K'){Kx=i;Ky=j;}else if(maze[i][j]=='S'){snakes[k++]={i,j};}}ans=bfs();if(ans==-1){printf("impossible\n");}else{printf("%d\n",ans);}}return 0;
}

这篇关于[状态压缩 广搜BFS]Saving Tang Monk的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1081173

相关文章

Qt实现文件的压缩和解压缩操作

《Qt实现文件的压缩和解压缩操作》这篇文章主要为大家详细介绍了如何使用Qt库中的QZipReader和QZipWriter实现文件的压缩和解压缩功能,文中的示例代码简洁易懂,需要的可以参考一下... 目录一、实现方式二、具体步骤1、在.pro文件中添加模块gui-private2、通过QObject方式创建

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

hdu1565(状态压缩)

本人第一道ac的状态压缩dp,这题的数据非常水,很容易过 题意:在n*n的矩阵中选数字使得不存在任意两个数字相邻,求最大值 解题思路: 一、因为在1<<20中有很多状态是无效的,所以第一步是选择有效状态,存到cnt[]数组中 二、dp[i][j]表示到第i行的状态cnt[j]所能得到的最大值,状态转移方程dp[i][j] = max(dp[i][j],dp[i-1][k]) ,其中k满足c

poj 2195 bfs+有流量限制的最小费用流

题意: 给一张n * m(100 * 100)的图,图中” . " 代表空地, “ M ” 代表人, “ H ” 代表家。 现在,要你安排每个人从他所在的地方移动到家里,每移动一格的消耗是1,求最小的消耗。 人可以移动到家的那一格但是不进去。 解析: 先用bfs搞出每个M与每个H的距离。 然后就是网络流的建图过程了,先抽象出源点s和汇点t。 令源点与每个人相连,容量为1,费用为

状态dp总结

zoj 3631  N 个数中选若干数和(只能选一次)<=M 的最大值 const int Max_N = 38 ;int a[1<<16] , b[1<<16] , x[Max_N] , e[Max_N] ;void GetNum(int g[] , int n , int s[] , int &m){ int i , j , t ;m = 0 ;for(i = 0 ;

hdu3006状态dp

给你n个集合。集合中均为数字且数字的范围在[1,m]内。m<=14。现在问用这些集合能组成多少个集合自己本身也算。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.Inp

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

实例:如何统计当前主机的连接状态和连接数

统计当前主机的连接状态和连接数 在 Linux 中,可使用 ss 命令来查看主机的网络连接状态。以下是统计当前主机连接状态和连接主机数量的具体操作。 1. 统计当前主机的连接状态 使用 ss 命令结合 grep、cut、sort 和 uniq 命令来统计当前主机的 TCP 连接状态。 ss -nta | grep -v '^State' | cut -d " " -f 1 | sort |

POJ 3057 最大二分匹配+bfs + 二分

SampleInput35 5XXDXXX...XD...XX...DXXXXX5 12XXXXXXXXXXXXX..........DX.XXXXXXXXXXX..........XXXXXXXXXXXXX5 5XDXXXX.X.DXX.XXD.X.XXXXDXSampleOutput321impossible