hdu 4634 Swipe Bo bfs+状态压缩
状态压缩记录当前拿到了哪些钥匙, 然后暴力搜索。
搞了好几个小时, 一开始也不知道哪里错了, 最后A了也不知道一开始哪里有问题。
- #include <iostream>
- #include <vector>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <map>
- #include <set>
- #include <string>
- #include <queue>
- #include <stack>
- #include <bitset>
- using namespace std;
- #define pb(x) push_back(x)
- #define ll long long
- #define mk(x, y) make_pair(x, y)
- #define lson l, m, rt<<1
- #define mem(a) memset(a, 0, sizeof(a))
- #define rson m+1, r, rt<<1|1
- #define mem1(a) memset(a, -1, sizeof(a))
- #define mem2(a) memset(a, 0x3f, sizeof(a))
- #define rep(i, n, a) for(int i = a; i<n; i++)
- #define fi first
- #define se second
- typedef pair<int, int> pll;
- const double PI = acos(-1.0);
- const double eps = 1e-;
- const int mod = 1e9+;
- const int inf = ;
- const int dir[][] = { {-, }, {, }, {, -}, {, } };
- const int maxn = ;
- int n, m, cnt, g[maxn][maxn];
- bool vis[maxn][maxn][<<][], used[maxn][maxn][<<];
- char s[maxn][maxn];
- struct node
- {
- int x, y, step, key;
- node(){}
- node(int _x, int _y, int _step, int _key): x(_x), y(_y), step(_step), key(_key){}
- };
- queue <node> q;
- int check(int x, int y) {
- if(x>=&&x<n&&y>=&&y<m)
- return ;
- return ;
- }
- int judge(int x, int y) {
- if(x == )
- return ;
- if(x == -)
- return ;
- if(y == )
- return ;
- if(y == -)
- return ;
- }
- void bfs(int x, int y) {
- mem(vis);
- mem(used);
- while(!q.empty())
- q.pop();
- used[x][y][] = ;
- q.push(node(x, y, , ));
- while(!q.empty()) {
- node temp = q.front(); q.pop();
- int x = temp.x, y = temp.y, tmpx, tmpy;
- for(int i = ; i < ; i++) {
- int dirx = dir[i][], diry = dir[i][];
- tmpx = x, tmpy = y;
- int key = temp.key;
- node tmp = temp;
- if(!check(tmpx+dirx, tmpy+diry) || s[tmpx+dirx][tmpy+diry]=='#')
- continue;
- while() {
- if(s[tmpx][tmpy] == 'L')
- dirx = , diry = -;
- if(s[tmpx][tmpy] == 'R')
- dirx = , diry = ;
- if(s[tmpx][tmpy] == 'U')
- dirx = -, diry = ;
- if(s[tmpx][tmpy] == 'D')
- dirx = , diry = ;
- int tmpdir = judge(dirx, diry);
- if(vis[tmpx][tmpy][tmp.key][tmpdir])
- break;
- vis[tmpx][tmpy][tmp.key][tmpdir] = ;
- if(s[tmpx][tmpy] == 'E' && key == (<<cnt)-) {
- printf("%d\n", temp.step+);
- return ;
- }
- if(s[tmpx][tmpy] == 'K')
- key |= g[tmpx][tmpy];
- if(check(tmpx+dirx, tmpy+diry)) {
- if(s[tmpx+dirx][tmpy+diry] == '#') {
- tmp.x = tmpx;
- tmp.y = tmpy;
- tmp.step++;
- tmp.key = key;
- q.push(tmp);
- used[tmpx][tmpy][tmp.key] = ;
- break;
- } else {
- tmpx += dirx;
- tmpy += diry;
- }
- } else {
- break;
- }
- }
- }
- }
- puts("-1");
- return ;
- }
- void solve() {
- cnt = ;
- int x, y;
- for(int i = ; i < n; i++) {
- for(int j = ; j < m; j++) {
- if(s[i][j] == 'S')
- x = i, y = j;
- if(s[i][j] == 'K') {
- g[i][j] = <<cnt;
- cnt++;
- }
- }
- }
- bfs(x, y);
- }
- void read() {
- for(int i = ; i < n; i++)
- scanf("%s", s[i]);
- }
- int main()
- {
- while(scanf("%d%d", &n, &m)!=EOF) {
- read();
- solve();
- }
- return ;
- }
hdu 4634 Swipe Bo bfs+状态压缩的更多相关文章
- 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 ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- hdu 4634 Swipe Bo 搜索
典型的bfs模拟 (广度优先搜索) ,不过有好多细节要注意,比如图中如果是 R# 走到这个R的话就无限往右走了,这样就挂了~肯定到不了出口.还有一种容易造成死循环的,比如 #E## DLLL D. ...
- HDU 4634 Swipe Bo 状态压缩+BFS最短路
将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...
- hdu 4845 : 拯救大兵瑞恩 (bfs+状态压缩)
题目链接 #include<bits/stdc++.h> using namespace std; typedef long long LL; int n,m,p,s,k; ,,,-}; ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
随机推荐
- 通过设置cookie实现单点登录
最近要做个登录一个客户端跳转到另一个网站不用再登录,有两种方法,第一种就是写接口通过客户端传值账号直接到目标网站,另一种是写入cookie到目标网站.由于目标网站之前就是通过cookie实现单点登录, ...
- SET STATISTICS IO和SET STATISTICS TIME 在SQL Server查询性能优化中的作用
近段时间以来,一直在探究SQL Server查询性能的问题,当然也漫无目的的查找了很多资料,也从网上的大神们的文章中学到了很多,在这里,向各位大神致敬.正是受大神们无私奉献精神的影响,所以小弟也作为回 ...
- Java面试题整理(题目内容非原创)
面试题分类: 1.java 基础面试题 Java基础中对于io 中文件的读.写,util中的list map set这些要分清楚 还有线程.socket 都需要了解下 参考链接:http://blog ...
- JSP 适配手机屏幕
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scal ...
- [Linked List]Remove Linked List Elements
Total Accepted: 43183 Total Submissions: 160460 Difficulty: Easy Remove all elements from a linked l ...
- linux 中ls命令函数
#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<dirent.h> ...
- 矩阵乘法的MPI并行计算
1.问题描述 矩阵乘法问题描述如下: 给定矩阵A和B,其中A是m*p大小矩阵,B是p*n大小的矩阵.求C = A*B. 求解这个问题最简单的算法是遍历A的行和B的列,求得C的相应元素,时间复杂度O(m ...
- 在 WinForm 中打开页面采用POST方式传参http。可以多个参数传递,返回json字符串
//调用方法 Dictionary<string, string> postData = new Dictionary<string, string>(); postData. ...
- 使用HAProxy、PHP、Redis和MySQL支撑每周10亿请求
在公司的发展中,保证服务器的可扩展性对于扩大企业的市场需要具有重要作用,因此,这对架构师提出了一定的要求.Octivi联合创始人兼软件架构师Antoni Orfin将向你介绍一个非常简单的架构,使用H ...
- 将Eclipse代码导入到Android Studio的两种方式
转: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0104/2259.html 说到使用Android Studio,除了新建 ...