http://acm.hdu.edu.cn/showproblem.php?pid=5094

给出n*m矩阵

给出k个障碍,两坐标之间存在墙或门,门最多10种,状压可搞

给出s个钥匙位置及编号,相应的钥匙开相应的门,求从1,1到n,m的最短时间,不能到底则输出-1

这里有一个大坑:有可能同一个位置有多个门或者多个钥匙...

这么坑大丈夫?

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
typedef unsigned long long ULL;
const int modo = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int inf = 0x3fffffff;
const LL _inf = 1e18;
const int maxn = 55,maxm = 1<<12;
int n,m,p;
bool vis[maxn][maxn][maxm];
int g[maxn][maxn][maxn][maxn],key[maxn][maxn];//0up1down2left3right
int b[12];
struct node{
int x,y,st,t;
node(){};
node(int xx,int yy,int _st,int tt):x(xx),y(yy),st(_st),t(tt){};
bool operator < (const node &a)const{
return a.t < t;
}
};
int dx[] = {0,0,-1,1},
dy[] = {-1,1,0,0};
bool in(int x,int y)
{
return 1 <= x && x<=n && 1 <= y && y <= m;
}
void bfs()
{
priority_queue<node> q;
q.push(node(1,1,key[1][1],0));
vis[1][1][key[1][1]] = 1; while(!q.empty()){
node cur = q.top();
q.pop();
if(cur.x == n && cur.y == m){
//cout<<cur.x<<','<<cur.y<<':';
printf("%d\n",cur.t);
return;
}
int x = cur.x,y = cur.y,t = cur.t,st = cur.st;
//cout<<x<<'.'<<y<<':'<<t<<endl;
for(int i = 0;i < 4;++i){
int tx = x + dx[i],ty = y + dy[i];
if(!in(tx,ty) || g[x][y][tx][ty] & 1 == 1)continue;
if(g[x][y][tx][ty] && !(st & g[x][y][tx][ty]))continue;
int _st = st | key[tx][ty];
if(!vis[tx][ty][_st]){
vis[tx][ty][_st] = 1;
q.push(node(tx,ty,_st,t+1));
}
}
}
puts("-1");
}
void init()
{
for(int i = 0;i < 12;++i)
b[i] = 1<<i;
}
void work()
{
clr0(vis),clr0(key);
clr0(g);
int k,s,x,y,q,x1,y1,x2,y2,st;
RD(k);
while(k--){
RD2(x1,y1),RD3(x2,y2,st);
g[x1][y1][x2][y2] |= b[st];
g[x2][y2][x1][y1] |= b[st];
}
RD(s);
while(s--){
RD3(x,y,q);
key[x][y] |= b[q];
}
bfs();
return ;
}
int main()
{
init();
while(~RD3(n,m,p)){
work();
}
return 0;
}

hdu 5094 状压bfs+深坑的更多相关文章

  1. hdu 4845 状压bfs(分层思想)

    拯救大兵瑞恩 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

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

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

  3. 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) ...

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

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

  5. POJ 1324 Holedox Moving (状压BFS)

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

  6. HDU 4778 状压DP

    一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...

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

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

  8. 状压BFS

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

  9. HDU 3001 状压DP

    有道状压题用了搜索被队友骂还能不能好好训练了,, hdu 3001 经典的状压dp 大概题意..有n个城市 m个道路  成了一个有向图.n<=10: 然后这个人想去旅行.有个超人开始可以把他扔到 ...

随机推荐

  1. PAT 1027 打印沙漏(20)(思路)

    1027 打印沙漏(20)(20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** ***** 所谓& ...

  2. RDMA的原理、传输与Verbs

    RDMA的原理.传输与Verbs   RDMA最早专属于infiniband架构.在网络融合的大趋势下出现的RoCE,使高速.超低延时.极低cpu使用率的RDMA得以部署在目前使用最广泛的以太网上.  ...

  3. 抽屉效果几大github第三方库

    首先感谢董铂然博客园,鄙人收藏学习之用,如有朋友看到.有需要请直接前往董铂然博客园本文, 请点击查看原文 在公司项目新版本方案选择中,对主导航中要使用的抽屉效果进行了调研.主要原因是旧的项目中所用的库 ...

  4. HDU 3861.The King’s Problem 强联通分量+最小路径覆盖

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. QT学习之路(1):彩票绝对不中模拟器

    //============================================//绝对不中,彩票开奖模拟器#include "mainwindow.h"#includ ...

  6. Java 中转换为String类型的四种方法

    1. 使用 String 的构造方法,用于 byte[], char[], StringBuffer, StringBuilder 类型 2. 使用 String 的静态方法 valueOf() 推荐 ...

  7. Spring IOC(四)FactoryBean

    Spring IOC(四)FactoryBean Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一般情况下,Spring ...

  8. PHP数组对象互转

    //数组转对象 function array2object($array) { if (is_array($array)) { $obj = new StdClass(); foreach ($arr ...

  9. PHP去除重复的数组数据

    PHP去除重复的数组数据 <?php $input = array("a" => "green","", "red&q ...

  10. python+selenium—webdriver入门(一)

    一.浏览器最大化 二.设置浏览器分辨率大小 三.打印页面title 四.打印URL 五.控制浏览器前进或后退 #!/usr/bin/env python#-*- coding:utf-8 -*- fr ...