1. 程式人生 > >拯救大兵瑞恩 HDU - 4845(狀壓bfs || 分層最短路)

拯救大兵瑞恩 HDU - 4845(狀壓bfs || 分層最短路)

sca -- bit 一個 signed space base {} out

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 || 分層最短路)