标签:ons man ane type fast mic 方向 inpu get
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 264 Accepted Submission(s): 106
题目链接:HDU 4845
很显然用三元组$(x,y,state)$表示当前x、y坐标,获取的钥匙情况,而且这样是可以唯一确定状态的,然后钥匙获取情况用状态压缩表示即可
代码:
#include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define fin(name) freopen(name,"r",stdin) #define fout(name) freopen(name,"w",stdout) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const int N = 16; struct info { int x, y; int keyst, step; info() {} info(int _x, int _y, int _keyst, int _step): x(_x), y(_y), keyst(_keyst), step(_step) {} }; int dir[4][2] = {{1, 0}, {0, 1}, { -1, 0}, {0, -1}}; int door[N][N][N][N]; int key[N][N]; int d[N][N][1 << 11]; bool vis[N][N][1 << 11]; int n, m, p, k, s; void init() { CLR(door, 0); CLR(key, 0); CLR(d, INF); CLR(vis, false); } inline bool check(const int &x, const int &y) { return x >= 1 && x <= n && y >= 1 && y <= m; } void bfs() { queue<info>Q; info S(1, 1, 0 | key[1][1], 0); Q.push(S); vis[S.x][S.y][S.keyst] = true; d[S.x][S.y][S.keyst] = 1; while (!Q.empty()) { info u = Q.front(); Q.pop(); for (int i = 0; i < 4; ++i) { int vx = u.x + dir[i][0]; int vy = u.y + dir[i][1]; if (!check(vx, vy)) continue; int need = door[u.x][u.y][vx][vy]; if (need == -1) continue; if ((need == 0 || (u.keyst & (1 << need)))) { int vkeyst = u.keyst | (key[vx][vy]); if (!vis[vx][vy][vkeyst]) { vis[vx][vy][vkeyst] = true; d[vx][vy][vkeyst] = u.step + 1; Q.push(info(vx, vy, vkeyst, d[vx][vy][vkeyst])); } } } } } int main(void) { int i; while (~scanf("%d%d%d", &n, &m, &p)) { init(); scanf("%d", &k); for (i = 0; i < k; ++i) { int x1, x2, y1, y2, g; scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &g); if (!g) door[x1][y1][x2][y2] = door[x2][y2][x1][y1] = -1; else door[x1][y1][x2][y2] = door[x2][y2][x1][y1] = g; } scanf("%d", &s); for (i = 0; i < s; ++i) { int x1, y1, qi; scanf("%d%d%d", &x1, &y1, &qi); key[x1][y1] |= (1 << qi); } bfs(); int ans = INF; int Alst = (1 << (p + 1)); for (i = 0; i < Alst; ++i) if (d[n][m][i] < ans) ans = d[n][m][i]; printf("%d\n", ans == INF ? -1 : ans); } return 0; }
标签:ons man ane type fast mic 方向 inpu get
原文地址:http://www.cnblogs.com/Blackops/p/7217062.html