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. VMware虚拟机下安装RedHat Linux 9.0

    从这一篇文章开始我和大家一起学习Linux系统.不管是什么样的系统,必须安装上才能谈使用对吧. Linux版本 安装Linux之前需要了解一下Linux系统的安装版本. Linux的版本分为内核版本和 ...

  2. 九度OJ 1037:Powerful Calculator(强大的计算器) (大整数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1821 解决:528 题目描述: Today, facing the rapid development of business, SJTU ...

  3. wx.onNetworkStatusChange(function (res) 监听网络状态变化 实践方案

    网络状态 · 小程序 https://developers.weixin.qq.com/miniprogram/dev/api/device.html#wxonnetworkstatuschangec ...

  4. distributed OSGI demo

    今天继续<OSGi原理与最佳实践>.看到第四章.做 HelloWorld-cxf 的样例 照着样例敲来着,整个样例敲完了,执行.一直报错, ----------------这里是解决方法- ...

  5. Python爬虫一些操作headers与cookies的便捷工具

    本篇文章主要是爬虫中常用的便捷处理方法整理,转载请注明出处 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-08-14 13: ...

  6. wireshark 学习 3 display filter

    过滤信息,得到想要的帧进行分析. http://www.networkcomputing.com/networking/wifi-troubleshooting-using-wireshark/155 ...

  7. Data Structure Binary Tree: Check for Children Sum Property in a Binary Tree

    http://www.geeksforgeeks.org/check-for-children-sum-property-in-a-binary-tree/ #include <iostream ...

  8. http://blog.csdn.net/renfufei/article/details/37725057/

    版权声明:本文为博主原创文章,未经博主允许不得转载. 原创:http://blog.csdn.net/renfufei/article/details/37725057/ 说明: 首先,你需要注册一个 ...

  9. LightOJ - 1287 Where to Run —— 期望、状压DP

    题目链接:https://vjudge.net/problem/LightOJ-1287 1287 - Where to Run    PDF (English) Statistics Forum T ...

  10. html5--1.3 元素的概念与3个常用标签

    html5--1.3 元素的概念与3个常用标签 学习要点 1.元素的概念 2.3个常用的标签 HTML 元素指的是从开始标签到结束标签的所有代码. 开始标签 元素内容 结束标签 <h1> ...