HDU 1429 胜利大逃亡(续)(状压bfs)
来源:互联网 发布:广电机顶盒破解网络 编辑:程序博客网 时间:2024/06/05 03:23
与其他bfs不同的是这种题需要用二进制来保存钥匙这个状态,需要多用一维数组来保存。。
可以通过位运算 | “拾取钥匙”, & “匹配钥匙”。
#include "string"#include "iostream"#include "cstdio"#include "cmath"#include "set"#include "queue"#include "vector"#include "cctype"#include "sstream"#include "cstdlib"#include "cstring"#include "stack"//#include "ctime"#include "algorithm"#define pa pair<int,int>#define Pi M_PI#define INF 0x3f3f3f3f#define INFL 0x3f3f3f3f3f3f3f3fLLusing namespace std;typedef long long LL;const int M=55;struct Node{ int x, y; int kind; int t;} star;int n, m, time;int dis[4][2] = {1, 0, 0, -1, -1, 0, 0, 1};char map[21][21];bool mark[20][20][1100];int BFS(){ int i, j, kind, temp, t; struct Node node, next; queue<Node> Q; star.kind = 0; star.t = 0; Q.push(star); mark[star.x][star.y][0] = true; while(!Q.empty()) { node = Q.front(); Q.pop(); kind = node.kind; t = node.t; if( t % time == 0 && t != 0) { node.x = star.x; node.y = star.y; } if( map[node.x][node.y] == '^') return node.t; for( i = 0 ; i < 4 ; i++) { next.x = node.x + dis[i][0]; next.y = node.y + dis[i][1]; next.kind = kind; if( next.x < 0 || next.y < 0 || next.x >= n || next.y >= m) continue; if( map[next.x][next.y] == '*' || mark[next.x][next.y][next.kind] == true) continue; next.t = t + 1; if( islower(map[next.x][next.y])) { j = map[next.x][next.y] - 'a'; if( (kind & (1 << j) ) == 0) //存储钥匙 next.kind = kind + (1 << j); } else if( isupper( map[next.x][next.y]) ) { j = map[next.x][next.y] - 'A'; temp = kind >> j; if( (temp & 1 ) == 0) //判断钥匙能匹配不 continue; next.kind = kind; } mark[next.x][next.y][next.kind] = true; Q.push(next); } } return -1;}int main(){ int i, j, k, ans; k = 0; while(~ scanf("%d%d%d", &n, &m, &time)) { memset( mark, 0, sizeof(mark)); for( i = 0 ; i < n ; i++) { for( j = 0 ; j < m ; j++) { cin>>map[i][j]; if( map[i][j] == '@') { star.x=i; star.y=j; } } } ans = BFS(); printf("%d\n", ans); }}
0 0
- HDU 1429 胜利大逃亡(续)(状压bfs)
- hdu 1429 胜利大逃亡(续)(BFS+状压)
- HDU 1429 状压bfs 胜利大逃亡
- [BFS] HDU 1429 胜利大逃亡(续)
- HDU 1429 胜利大逃亡(续) (BFS )
- HDU 1429胜利大逃亡(续) 【bfs+状压】
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
- hdu 1429 胜利大逃亡(续)(BFS+位压缩)
- HDU 1429 胜利大逃亡(续)(bfs)
- hdu 1429 胜利大逃亡(续) 状态压缩+BFS
- HDU 1429胜利大逃亡(续) (bfs+状态压缩)
- HDU 1429 胜利大逃亡(续) (BFS+二进制)
- HDU 1429 胜利大逃亡(续) (状态压缩+BFS)
- HDU 1429 胜利大逃亡(续)(BFS+状态压缩)
- HDU-1429(胜利大逃亡续)-BFS+状态压缩
- hdu 1429 胜利大逃亡(续) bfs+状态压缩
- HDU 1429 胜利大逃亡(续)(BFS+状态压缩)
- HDU 1429 胜利大逃亡(续) (bfs+状态压缩)
- (五)java运算
- 使用 libevent 和 libev 提高网络应用性能——I/O模型演进变化史
- 读书笔记
- 蓝桥杯 制作表格 循环条件判断
- Boost.Asio基础(四)
- HDU 1429 胜利大逃亡(续)(状压bfs)
- UVA - 10023 Square root
- 解决:windows安装程序无法将windows配置为在此计算机的硬件上运行
- objective-c block 详解
- facebook pop 学习笔记
- Initializing Spring root WebApplicationContext
- TRANSACTION_SOURCE_ID 跟 TRANSACTION_SOURCE_TYPE_ID所关联的表的关系
- gt9xx.c和gt9xx.h文件分析
- 房间安排