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

给出n*m矩阵

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

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

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

这么坑大丈夫?

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <string>
  6. #include <queue>
  7. #include <map>
  8. #include <iostream>
  9. #include <algorithm>
  10. using namespace std;
  11. #define RD(x) scanf("%d",&x)
  12. #define RD2(x,y) scanf("%d%d",&x,&y)
  13. #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
  14. #define clr0(x) memset(x,0,sizeof(x))
  15. #define clr1(x) memset(x,-1,sizeof(x))
  16. #define eps 1e-9
  17. const double pi = acos(-1.0);
  18. typedef long long LL;
  19. typedef unsigned long long ULL;
  20. const int modo = 1e9 + 7;
  21. const int INF = 0x3f3f3f3f;
  22. const int inf = 0x3fffffff;
  23. const LL _inf = 1e18;
  24. const int maxn = 55,maxm = 1<<12;
  25. int n,m,p;
  26. bool vis[maxn][maxn][maxm];
  27. int g[maxn][maxn][maxn][maxn],key[maxn][maxn];//0up1down2left3right
  28. int b[12];
  29. struct node{
  30. int x,y,st,t;
  31. node(){};
  32. node(int xx,int yy,int _st,int tt):x(xx),y(yy),st(_st),t(tt){};
  33. bool operator < (const node &a)const{
  34. return a.t < t;
  35. }
  36. };
  37. int dx[] = {0,0,-1,1},
  38. dy[] = {-1,1,0,0};
  39. bool in(int x,int y)
  40. {
  41. return 1 <= x && x<=n && 1 <= y && y <= m;
  42. }
  43. void bfs()
  44. {
  45. priority_queue<node> q;
  46. q.push(node(1,1,key[1][1],0));
  47. vis[1][1][key[1][1]] = 1;
  48.  
  49. while(!q.empty()){
  50. node cur = q.top();
  51. q.pop();
  52. if(cur.x == n && cur.y == m){
  53. //cout<<cur.x<<','<<cur.y<<':';
  54. printf("%d\n",cur.t);
  55. return;
  56. }
  57. int x = cur.x,y = cur.y,t = cur.t,st = cur.st;
  58. //cout<<x<<'.'<<y<<':'<<t<<endl;
  59. for(int i = 0;i < 4;++i){
  60. int tx = x + dx[i],ty = y + dy[i];
  61. if(!in(tx,ty) || g[x][y][tx][ty] & 1 == 1)continue;
  62. if(g[x][y][tx][ty] && !(st & g[x][y][tx][ty]))continue;
  63. int _st = st | key[tx][ty];
  64. if(!vis[tx][ty][_st]){
  65. vis[tx][ty][_st] = 1;
  66. q.push(node(tx,ty,_st,t+1));
  67. }
  68. }
  69. }
  70. puts("-1");
  71. }
  72. void init()
  73. {
  74. for(int i = 0;i < 12;++i)
  75. b[i] = 1<<i;
  76. }
  77. void work()
  78. {
  79. clr0(vis),clr0(key);
  80. clr0(g);
  81. int k,s,x,y,q,x1,y1,x2,y2,st;
  82. RD(k);
  83. while(k--){
  84. RD2(x1,y1),RD3(x2,y2,st);
  85. g[x1][y1][x2][y2] |= b[st];
  86. g[x2][y2][x1][y1] |= b[st];
  87. }
  88. RD(s);
  89. while(s--){
  90. RD3(x,y,q);
  91. key[x][y] |= b[q];
  92. }
  93. bfs();
  94. return ;
  95. }
  96. int main()
  97. {
  98. init();
  99. while(~RD3(n,m,p)){
  100. work();
  101. }
  102. return 0;
  103. }

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. 去掉easyui datagrid内部虚线的方式。

    去掉easyui        datagrid内部虚线的方式.easyui datagrid的样式是统一写在样式文件中的,如果想要统一替换可以找对应的datagird样式文件中的以下部分.如果想要改 ...

  2. IIS7中的站点、应用程序和虚拟目录详细介绍

    IIS7中的站点.应用程序和虚拟目录详细介绍 这里说的不是如何解决路径重写或者如何配置的问题,而是阐述一下站点(site),应用程序(application)和虚拟目录 (virtual direct ...

  3. 改变dos的编码方式

    chcp 936 改变成 gbk chcp 65001 改成 utf-8 删除MySqlite文件 generic_x86:/data/data/com.example.lifen.sqlite/da ...

  4. XiaoKL学Python(D)argparse

    该文以Python 2为基础. 1. argparse简介 argparse使得编写用户友好的命令行接口更简单. argparse知道如何解析sys.argv. argparse 模块自动生成 “帮助 ...

  5. iOS设置图片名称、启动图片、防止TabBar图片和文字渲染

    设置App的名称 设置App的启动图片 需要注意点是,App要杀掉重启才能显示出启动图片 2种方法防止图片被渲染 1. vc02.tabBarItem.image = [UIImage imageNa ...

  6. dell E6400笔记本 centos7 安装无线网卡

    1.下载  驱动:http://download.csdn.net/detail/nj4506/9752842 2.解压: make  make install 3.将wk.ko拷贝到 /lib/mo ...

  7. regexper-正则表达式检验

    https://regexper.com 例如:输入 \s.?\w{1,3}(\d(\W)(#?\d{1..4}))

  8. 【转载】为什么任何随便输入的账号使用SYSDBA权限都能登陆oracle

    其实简单点就是检查一下你的机器有没有一个ora_dba用户组,而且你登陆os的用户是否在这个组里,有的话问题的原因就找到了,下面是转的高手的介绍 本文环境配置:Oracle10gR2,Windows ...

  9. JDBC连接MySql,配置url报错

    使用JDBC连接MySql时出现:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one ...

  10. 关于ueditor 在struts2 中 上传图片 ,未找到上传文件 问题的解决方法

    问题原因: ueditor 上传图片需请求imageUp.jsp文件,struts2 自带的拦截器(/*)把所有请求的文件都做了处理,所以导致无法上传图片. 解决方法: 方法一:自定义拦截器,让它在请 ...