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. linux中的设备类型

    loop设备 loop设备 一.参考命令[root@localhost a]# losetup usage:  losetup loop_device                          ...

  2. 用户态处理arp、ndisc neighbour solication 报文

    问题背景: 想要协议栈给不是接口ip的报文,ipv4回复arp request,ipv6回复 ndisc solication. #include <stdio.h> //调用该函数成为一 ...

  3. 将某视图View转换为UIImage

    + (UIImage *)getSharedScreenView{ UIWindow *screenWindow = [[UIApplication sharedApplication]keyWind ...

  4. Linux renew ip command

    $ sudo dhclient -r    //release ip 释放IP$ sudo dhclient       //获取IP Now obtain fresh IP:$ sudo dhcli ...

  5. [Hbase]Hbase章1 Hbase框架及基本概念

    Hbase框架介绍 HBase是一个分布式的.面向列的开源数据库. 不同点: l  和一般的关系数据库不同,hbase是一个适合于非结构化数据存储的数据库. l  Hbase是基于列而不是基于行的模式 ...

  6. ubuntu samba共享后windows读写文件都是以nogroup问题

    添加smb账号 sudo smbpasswd -a xxx 如果报错:Failed to add entry for user xxx 则是因为这个账号不存在 添加成功以后,过一会就可以重新登陆了(u ...

  7. canvas 实现烟花效果

    一:创建画布 <canvas width="600" height="600" id="canvas" style="bor ...

  8. kbmmw 中XML 操作入门

    delphi 很早以前就自带了xml 的操作,最新版里面有三种XML 解释器,一种是MSXML,看名字就知道 这个是微软自带的,这个据delphi 官方称是速度是最快的,但是只能在windows 上使 ...

  9. python生成器初步了解

    一.生成器 生成器的本质就是迭代器    一个一个的创建对象   1.创建生成器的方式:    1.生成器函数 2.通过生成器表达式来获取生成器 3.类型转换 2.优点 节省内存 ,生成器本身就是代码 ...

  10. 2018.11.01 NOIP训练 木棒分组(搜索+剪枝)

    传送门 测试搜索的时候状态定义错了233. 我们把木棒从大到小排序. 然后保证每一组搜到的木棒出现的长度是从大到小递减的. 直接定义现在搜的木棒从什么位置开始,当前这一组的总长度,之前几组的总长度. ...