题目链接

状态压缩记录当前拿到了哪些钥匙, 然后暴力搜索。

搞了好几个小时, 一开始也不知道哪里错了, 最后A了也不知道一开始哪里有问题。

  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <map>
  8. #include <set>
  9. #include <string>
  10. #include <queue>
  11. #include <stack>
  12. #include <bitset>
  13. using namespace std;
  14. #define pb(x) push_back(x)
  15. #define ll long long
  16. #define mk(x, y) make_pair(x, y)
  17. #define lson l, m, rt<<1
  18. #define mem(a) memset(a, 0, sizeof(a))
  19. #define rson m+1, r, rt<<1|1
  20. #define mem1(a) memset(a, -1, sizeof(a))
  21. #define mem2(a) memset(a, 0x3f, sizeof(a))
  22. #define rep(i, n, a) for(int i = a; i<n; i++)
  23. #define fi first
  24. #define se second
  25. typedef pair<int, int> pll;
  26. const double PI = acos(-1.0);
  27. const double eps = 1e-;
  28. const int mod = 1e9+;
  29. const int inf = ;
  30. const int dir[][] = { {-, }, {, }, {, -}, {, } };
  31. const int maxn = ;
  32. int n, m, cnt, g[maxn][maxn];
  33. bool vis[maxn][maxn][<<][], used[maxn][maxn][<<];
  34. char s[maxn][maxn];
  35. struct node
  36. {
  37. int x, y, step, key;
  38. node(){}
  39. node(int _x, int _y, int _step, int _key): x(_x), y(_y), step(_step), key(_key){}
  40. };
  41. queue <node> q;
  42. int check(int x, int y) {
  43. if(x>=&&x<n&&y>=&&y<m)
  44. return ;
  45. return ;
  46. }
  47. int judge(int x, int y) {
  48. if(x == )
  49. return ;
  50. if(x == -)
  51. return ;
  52. if(y == )
  53. return ;
  54. if(y == -)
  55. return ;
  56. }
  57. void bfs(int x, int y) {
  58. mem(vis);
  59. mem(used);
  60. while(!q.empty())
  61. q.pop();
  62. used[x][y][] = ;
  63. q.push(node(x, y, , ));
  64. while(!q.empty()) {
  65. node temp = q.front(); q.pop();
  66. int x = temp.x, y = temp.y, tmpx, tmpy;
  67. for(int i = ; i < ; i++) {
  68. int dirx = dir[i][], diry = dir[i][];
  69. tmpx = x, tmpy = y;
  70. int key = temp.key;
  71. node tmp = temp;
  72. if(!check(tmpx+dirx, tmpy+diry) || s[tmpx+dirx][tmpy+diry]=='#')
  73. continue;
  74. while() {
  75. if(s[tmpx][tmpy] == 'L')
  76. dirx = , diry = -;
  77. if(s[tmpx][tmpy] == 'R')
  78. dirx = , diry = ;
  79. if(s[tmpx][tmpy] == 'U')
  80. dirx = -, diry = ;
  81. if(s[tmpx][tmpy] == 'D')
  82. dirx = , diry = ;
  83. int tmpdir = judge(dirx, diry);
  84. if(vis[tmpx][tmpy][tmp.key][tmpdir])
  85. break;
  86. vis[tmpx][tmpy][tmp.key][tmpdir] = ;
  87. if(s[tmpx][tmpy] == 'E' && key == (<<cnt)-) {
  88. printf("%d\n", temp.step+);
  89. return ;
  90. }
  91. if(s[tmpx][tmpy] == 'K')
  92. key |= g[tmpx][tmpy];
  93. if(check(tmpx+dirx, tmpy+diry)) {
  94. if(s[tmpx+dirx][tmpy+diry] == '#') {
  95. tmp.x = tmpx;
  96. tmp.y = tmpy;
  97. tmp.step++;
  98. tmp.key = key;
  99. q.push(tmp);
  100. used[tmpx][tmpy][tmp.key] = ;
  101. break;
  102. } else {
  103. tmpx += dirx;
  104. tmpy += diry;
  105. }
  106. } else {
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. puts("-1");
  113. return ;
  114. }
  115. void solve() {
  116. cnt = ;
  117. int x, y;
  118. for(int i = ; i < n; i++) {
  119. for(int j = ; j < m; j++) {
  120. if(s[i][j] == 'S')
  121. x = i, y = j;
  122. if(s[i][j] == 'K') {
  123. g[i][j] = <<cnt;
  124. cnt++;
  125. }
  126. }
  127. }
  128. bfs(x, y);
  129. }
  130. void read() {
  131. for(int i = ; i < n; i++)
  132. scanf("%s", s[i]);
  133. }
  134. int main()
  135. {
  136. while(scanf("%d%d", &n, &m)!=EOF) {
  137. read();
  138. solve();
  139. }
  140. return ;
  141. }

hdu 4634 Swipe Bo bfs+状态压缩的更多相关文章

  1. HDU 4634 Swipe Bo (2013多校4 1003 搜索)

    Swipe Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

  3. hdu 4634 Swipe Bo 搜索

    典型的bfs模拟 (广度优先搜索) ,不过有好多细节要注意,比如图中如果是  R#  走到这个R的话就无限往右走了,这样就挂了~肯定到不了出口.还有一种容易造成死循环的,比如 #E## DLLL D. ...

  4. HDU 4634 Swipe Bo 状态压缩+BFS最短路

    将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...

  5. hdu 4845 : 拯救大兵瑞恩 (bfs+状态压缩)

    题目链接 #include<bits/stdc++.h> using namespace std; typedef long long LL; int n,m,p,s,k; ,,,-}; ...

  6. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  7. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  8. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  9. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

随机推荐

  1. 通过设置cookie实现单点登录

    最近要做个登录一个客户端跳转到另一个网站不用再登录,有两种方法,第一种就是写接口通过客户端传值账号直接到目标网站,另一种是写入cookie到目标网站.由于目标网站之前就是通过cookie实现单点登录, ...

  2. SET STATISTICS IO和SET STATISTICS TIME 在SQL Server查询性能优化中的作用

    近段时间以来,一直在探究SQL Server查询性能的问题,当然也漫无目的的查找了很多资料,也从网上的大神们的文章中学到了很多,在这里,向各位大神致敬.正是受大神们无私奉献精神的影响,所以小弟也作为回 ...

  3. Java面试题整理(题目内容非原创)

    面试题分类: 1.java 基础面试题 Java基础中对于io 中文件的读.写,util中的list map set这些要分清楚 还有线程.socket 都需要了解下 参考链接:http://blog ...

  4. JSP 适配手机屏幕

    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scal ...

  5. [Linked List]Remove Linked List Elements

    Total Accepted: 43183 Total Submissions: 160460 Difficulty: Easy Remove all elements from a linked l ...

  6. linux 中ls命令函数

    #include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<dirent.h> ...

  7. 矩阵乘法的MPI并行计算

    1.问题描述 矩阵乘法问题描述如下: 给定矩阵A和B,其中A是m*p大小矩阵,B是p*n大小的矩阵.求C = A*B. 求解这个问题最简单的算法是遍历A的行和B的列,求得C的相应元素,时间复杂度O(m ...

  8. 在 WinForm 中打开页面采用POST方式传参http。可以多个参数传递,返回json字符串

    //调用方法 Dictionary<string, string> postData = new Dictionary<string, string>(); postData. ...

  9. 使用HAProxy、PHP、Redis和MySQL支撑每周10亿请求

    在公司的发展中,保证服务器的可扩展性对于扩大企业的市场需要具有重要作用,因此,这对架构师提出了一定的要求.Octivi联合创始人兼软件架构师Antoni Orfin将向你介绍一个非常简单的架构,使用H ...

  10. 将Eclipse代码导入到Android Studio的两种方式

    转: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0104/2259.html 说到使用Android Studio,除了新建 ...