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. 使用active mq

    1 windows下使用active mq 1.1 下载active mq 1.2 点击根目录\bin\win64\activemq.bat运行 1.3 登陆查看 http://localhost:8 ...

  2. Webpack探索【12】--- externals详解

    本文主要讲externals相关内容. https://segmentfault.com/a/1190000012113011

  3. Java8系列之重新认识HashMap(转)

    转自美团电瓶技术团队:原文地址 简介 Java为数据结构中的映射定义了一个接口java.util.Map,此接口主要有四个常用的实现类,分别是HashMap.Hashtable.LinkedHashM ...

  4. LeetCode:有效的括号【20】

    LeetCode:有效的括号[20] 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. ...

  5. html(HyperText Markup Language)--超文本标记语言

    1.html的简介? * 什么是html? ** HyperText Markup Language:超文本标记语言,网页语言 ** 超文本:超出文本的范畴,使用html可以轻松实现这样操作: ** ...

  6. 大数据初级笔记二:Hadoop入门之Hadoop集群搭建

    Hadoop集群搭建 把环境全部准备好,包括编程环境. JDK安装 版本要求: 强烈建议使用64位的JDK版本,这样的优势在于JVM的能够访问到的最大内存就不受限制,基于后期可能会学习到Spark技术 ...

  7. 使用 HTML5 的 IndexedDB API

    1. [代码]判断是否支持 IndexedDB     var indexedDB = window.indexedDB || window.webkitIndexedDB || window.moz ...

  8. css(4)

    类选择器和id选择器都有父子选择器. 在css文件中国,有时候为了简化样式,可以把相同的样式拿出来放在一起. display:inline display:block 行内元素里只能放行内元素,而块内 ...

  9. ffmpeg去水印

    1.用potplayer打开有水印的视频文件,截图一张待用.2.用IrfanView打开保存的图片,调整到100%大小,按住鼠标左键框选水印位置,记下标题“Selection:”右边的4组数字.3.f ...

  10. swoole的http服务

    PHP实现基于Swoole简单的HTTP服务器 引用Swoole官方定义: PHP语言的异步.并行.高性能网络通信框架,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户 ...