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. BOM心得-定时器

    写在前面的话:之前一直以为定时器的返回值是Object类型,所以timer初始化也是写null,今天发现返回值是number,进而发觉这个返回值代表的是定时器的索引,指代这是第几个定时器 个人觉得只用 ...

  2. 未能找到 CodeDom 提供程序类型“Microsoft.VJSharp.VJSharpCodeProvider,

    错误 1 未能找到 CodeDom 提供程序类型“Microsoft.VJSharp.VJSharpCodeProvider, VJSharpCodeProvider, Version=2.0.0.0 ...

  3. .net委托

    今天要学的是委托 委托的基本形式 直接上代码 public delegate int AddDelegate(int x,int y); class Program { static void Mai ...

  4. 让delphi像C语言一样灵活

    lazarus是免费的delphi 拥有和c语言一样的灵活性,见代码: procedure TForm1.FormCreate(Sender: TObject);var p:PIntegerArray ...

  5. [Hbase]Hbase知识大全

    HBase简介 是一个构建在HDFS上的分布式列存储系统:HBase是基于Google BigTable模型开发的,典型的key/value系统:HBase是Apache Hadoop生态系统中的重要 ...

  6. 好像leeceode题目我的博客太长了,需要重新建立一个. leecode刷题第二个

    376. Wiggle Subsequence               自己没想出来,看了别人的分析. 主要是要分析出升序降序只跟临近的2个决定.虽然直觉上不是这样. 455. 分发饼干     ...

  7. openssl初步使用

    centos平台 md5.c #include <stdio.h> #include <string.h> #include <stdlib.h> //#inclu ...

  8. PHP连接SQLServer2012两例

    首先放上 PHP连接SQLServer的驱动下载地址 http://php.net/manual/zh/ref.pdo-sqlsrv.php 另外PHP for IIS管理工具 大家可以自己搜索一下 ...

  9. Windows 平台 (UWP)应用设计

    Make Your Apps Cooperate with Cross-App Communication :  https://rewards.msdn.microsoft.com/Challeng ...

  10. android开发笔记(2)

    我之前完成了SDK的安装,这次需要在eclipse中导入相关的控件. 一.下载ADT 在之前下载的网站上下载相关的ADT的压缩包. 二.在eclipse中进行导入 在eclipse中的Help-> ...