Maze

This story happened on the background of Star Trek.

Spock, the deputy captain of Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS.

The captain of Enterprise, James T. Kirk, had to fly to Qo’noS to rescue his deputy. Fortunately, he stole a map of the maze where Spock was put in exactly.

The maze is a rectangle, which has n rows vertically and m columns horizontally, in another words, that it is divided into n*m locations. An ordered pair (Row No., Column No.) represents a location in the maze. Kirk moves from current location to next costs 1 second. And he is able to move to next location if and only if:

Next location is adjacent to current Kirk’s location on up or down or left or right(4 directions) 
Open door is passable, but locked door is not. 
Kirk cannot pass a wall

There are p types of doors which are locked by default. A key is only capable of opening the same type of doors. Kirk has to get the key before opening corresponding doors, which wastes little time.

Initial location of Kirk was (1, 1) while Spock was on location of (n, m). Your task is to help Kirk find Spock as soon as possible.

InputThe input contains many test cases.

Each test case consists of several lines. Three integers are in the first line, which represent n, m and p respectively (1<= n, m <=50, 0<= p <=10). 
Only one integer k is listed in the second line, means the sum number of gates and walls, (0<= k <=500).

There are 5 integers in the following k lines, represents x i1, y i1, x i2, y i2, gi; when g i >=1, represents there is a gate of type gi between location (x i1, y i1) and (x i2, y i2); when g i = 0, represents there is a wall between location (x i1, yi1) and (x i2, y i2), ( | x i1 - x i2 | + | y i1 - y i2 |=1, 0<= g i <=p )

Following line is an integer S, represent the total number of keys in maze. (0<= S <=50).

There are three integers in the following S lines, represents x i1, y i1 and q irespectively. That means the key type of q i locates on location (x i1, y i1), (1<= qi<=p).OutputOutput the possible minimal second that Kirk could reach Spock.

If there is no possible plan, output -1. 
Sample Input

4 4 9
9
1 2 1 3 2
1 2 2 2 0
2 1 2 2 0
2 1 3 1 0
2 3 3 3 0
2 4 3 4 1
3 2 3 3 0
3 3 4 3 0
4 3 4 4 0
2
2 1 2
4 2 1

Sample Output

14

题目大意:给定一个棋盘,从(1,1)走到(n,m)有的任意两个格子之间的边视为:通路,门,墙。通路可以直接走,门必须早到相应的钥匙,墙永远不能通过。钥匙在一些给定点的格子中(同一个格子中可能有多把钥匙),问采取怎样的走法可以得到最少的移动步数

解题思路:b[x][y][sta]表示状态为s时到达(x,y)点是否已经到达过,key[x][y]表示钥匙的得到情况的状态。然后进行bfs即可

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#define MAX 55
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll; int a[MAX][MAX][MAX][MAX];
int b[MAX][MAX][(<<)+];
int key[MAX][MAX];
int t[][]={{,},{,},{-,},{,-}}; struct Node{
int x,y,sta,s;
}node;
queue<Node> q; int main()
{
int n,m,k,x,i,j;
int x1,y1,x2,y2;
while(~scanf("%d%d%d",&n,&m,&k)){
scanf("%d",&k);
memset(a,-,sizeof(a));
memset(b,,sizeof(b));
memset(key,,sizeof(key));
for(i=;i<=k;i++){
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&x);
a[x1][y1][x2][y2]=x;
a[x2][y2][x1][y1]=x;
}
scanf("%d",&k);
for(i=;i<=k;i++){
scanf("%d%d%d",&x1,&y1,&x);
key[x1][y1]|=<<(x-);
}
if(n==&&m==){
printf("0\n");
continue;
}
while(q.size()){
q.pop();
}
node.x=;
node.y=;
node.sta=;
node.s=;
q.push(node);
b[][][]=;
int f=;
while(q.size()){
for(i=;i<;i++){
int tx=q.front().x+t[i][];
int ty=q.front().y+t[i][];
if(tx<||ty<||tx>n||ty>m) continue;
int fx=q.front().x;
int fy=q.front().y;
int fsta=q.front().sta;
if(a[fx][fy][tx][ty]==) continue;
else if(a[fx][fy][tx][ty]>){
if(!(fsta&(<<(a[fx][fy][tx][ty]-)))) continue;
}
if(b[tx][ty][fsta]==) continue;
int fs=q.front().s;
if(tx==n&&ty==m){
f=fs+;
break;
}
node.x=tx;
node.y=ty;
node.sta=fsta|key[tx][ty];
node.s=fs+;
q.push(node);
b[tx][ty][node.sta]=;
}
if(f>) break;
q.pop();
}
if(f==) printf("-1\n");
else printf("%d\n",f);
}
return ;
}

HDU - 5094 Maze(状压+bfs)的更多相关文章

  1. 拯救大兵瑞恩 HDU - 4845(状压bfs || 分层最短路)

    1.状压bfs 这个状压体现在key上  我i们用把key状压一下  就能记录到一个点时 已经拥有的key的种类 ban[x1][y1][x2][y1]记录两个点之间的状态 是门 还是墙 还是啥都没有 ...

  2. HDU 4012 Paint on a Wall(状压+bfs)

    Paint on a Wall Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  3. HDU Stealing Harry Potter's Precious(状压BFS)

    状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...

  4. POJ 1324 Holedox Moving (状压BFS)

    POJ 1324 Holedox Moving (状压BFS) Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18091 Acc ...

  5. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  6. P2622 关灯问题II(状压bfs)

    P2622 关灯问题II 题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯——按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j] ...

  7. 状压BFS

    ​题意:1个机器人找几个垃圾,求出最短路径. 状压BFS,这道题不能用普通BFS二维vis标记数组去标记走过的路径,因为这题是可以往回走的,而且你也不能只记录垃圾的数量就可以了,因为它有可能重复走同一 ...

  8. hdu 5094 Maze (BFS+状压)

    题意: n*m的迷宫.多多要从(1,1)到达(n,m).每移动一步消耗1秒.有P种钥匙. 有K个门或墙.给出K个信息:x1,y1,x2,y2,gi    含义是(x1,y1)与(x2,y2)之间有gi ...

  9. hdu 5094 状压bfs+深坑

    http://acm.hdu.edu.cn/showproblem.php?pid=5094 给出n*m矩阵 给出k个障碍,两坐标之间存在墙或门,门最多10种,状压可搞 给出s个钥匙位置及编号,相应的 ...

  10. HDU 5094 Maze (状压)

    加一个维度,钥匙的状态,状压一下.n很小,钥匙也只有10个,bfs就好了. 忘了数组初始化.以后坚决不犯这种低级错误. #include<cstdio> #include<queue ...

随机推荐

  1. 使用Scrapy采集

    1.有些站点通过robot协议,防止scrapy爬取,就会出现如下问题: DEBUG: Forbidden by robots.txt: <GET http://baike.baidu.com/ ...

  2. 我的Android进阶之旅------>直接拿来用!最火的Android开源项目

    转载于CSDN,相关链接如下: http://www.csdn.net/article/2013-05-03/2815127-Android-open-source-projects http://w ...

  3. 【linux】如何查看进程运行在那颗cpu上

    这里介绍一种方法查看进程运行在哪个cpu上, 首先top 然后按字母:f 按字母:j 回车即可 其中P列表示进程运行在哪个CPU上

  4. 9patch图片

    9patch图片可直接缩放,放在drawable文件夹下就可以 右边和下边指定内容区域

  5. webpack热替换原理

    前期准备: const path = require('path'); const HtmlWebpackPlugin= require('html-webpack-plugin'); const C ...

  6. 使用aspnet_regiis.exe重新注册.NET Framework

    cd  C:\Windows\Microsoft.NET\Framework64\v4.0.30319  aspnet_regiis.exe -i     重新安装IIS以后,需要用aspnet_re ...

  7. 对于glut和freeglut的一点比较和在VS2013上的配置问题

    先大概说一下glut.h和freeglut.h 首先要知道openGL是只提供绘图,不管窗口的,所以你需要给它一个绘图的区域(openGL能跨平台也与此有些关系) glut.h和freeglut.h都 ...

  8. MysqL的root用户不允许远程连接,只能通过PHPMYADMIN

    解决方法:1.改表法 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 &q ...

  9. PHP的深copy和浅copy

    1.对象复制的由来 为什么对象会有“复制”这个概念,这与PHP5中对象的传值方式是密切相关的,让我们看看下面这段简单的代码 /** * 电视机类 */ class Television { /** * ...

  10. C# winform控件之PictureBox详解

    PictureBox表示用于显示图像的 Windows 图片框控件https://msdn.microsoft.com/zh-cn/library/system.windows.forms.pictu ...