标签:stack pop bit 没有 pre 最短路 color else air
1、状压bfs
这个状压体现在key上 我i们用把key状压一下 就能记录到一个点时 已经拥有的key的种类
ban[x1][y1][x2][y1]记录两个点之间的状态 是门 还是墙 还是啥都没有
inc[x][y]记录这个点所存储的钥匙 (可能不止一个 所以要用二进制)
vis[x][y][key] 标记当前点 在拥有的钥匙种类为key时是否走过
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define pd(a) printf("%d\n", a); #define plld(a) printf("%lld\n", a); #define pc(a) printf("%c\n", a); #define ps(a) printf("%s\n", a); #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 20, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff; int n, m, p, k, s; int ban[maxn][maxn][maxn][maxn], inc[maxn][maxn]; int vis[maxn][maxn][1 << 10 + 1]; int dis[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; struct node { int x, y, key, d; node(int x, int y, int key, int d) : x(x), y(y), key(key), d(d) {} }; int bfs() { queue<node> Q; mem(vis, 0); Q.push(node(1, 1, 0 | inc[1][1], 0)); vis[1][1][0 | inc[1][1]] = 1; while(!Q.empty()) { node is = Q.front(); Q.pop(); for(int i = 0; i < 4; i++) { node it(0, 0, 0, 0); it.x = is.x + dis[i][0]; it.y = is.y + dis[i][1]; if(it.x < 1 || it.x > n || it.y < 1 || it.y > m) continue; if(!ban[it.x][it.y][is.x][is.y]) continue; if(ban[it.x][it.y][is.x][is.y] == -1 || is.key & (1 << (ban[it.x][it.y][is.x][is.y] -1))) //如果下一个点与当前点没有墙或门 或者到当前点时已经有了进入下一个点的钥匙 { if(!inc[it.x][it.y]) it.key = is.key; //如果下一个点没有存储的钥匙 则直接把当前钥匙传递过去 else it.key = is.key | inc[it.x][it.y]; //如果有存储的钥匙 把存储的钥匙加上即可 if(!vis[it.x][it.y][it.key]) { vis[it.x][it.y][it.key] = 1; it.d = is.d + 1; Q.push(it); if(it.x == n && it.y == m) return it.d; } } } } return -1; } int main() { while(cin >> n >> m >> p >> k) { int x1, y1, x2, y2, G, Q; mem(ban, -1); mem(inc, 0); for(int i = 1; i <= k; i++) { cin >> x1 >> y1 >> x2 >> y2 >> G; ban[x1][y1][x2][y2] = ban[x2][y2][x1][y1] = G; } cin >> s; for(int i = 1; i <= s; i++) { cin >> x1 >> y1 >> Q; inc[x1][y1] |= (1 << (Q - 1)); } cout << bfs() << endl; } return 0; }
拯救大兵瑞恩 HDU - 4845(状压bfs || 分层最短路)
标签:stack pop bit 没有 pre 最短路 color else air
原文地址:https://www.cnblogs.com/WTSRUVF/p/9738827.html