hdoj 4198 Quick out of the Harbour 优先队列 + bfs
2012-04-07 18:41
453 查看
思路:对于普通迷宫求解,bfs可以到到唯一解,也是最优解。但对于不同点具有不同权值的地图就不一定了,很可能最优值会被pass掉,但使用优先队列的话,访问当前的点总是以前一个优先权最高的点访问的。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int N = 509; struct node { int x, y, val; node () {} node (int tx, int ty, int tval) { x = tx; y = ty; val = tval; } bool operator < (const node &x) const { return val > x.val; } }; priority_queue<node> pq; int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; bool vis ; char map ; int h, w, d, ans; void bfs() { node t,tmp; while (!pq.empty()) { t = pq.top(); pq.pop(); if (t.x == 0 || t.x == w-1 || t.y == 0 || t.y == h-1) { ans = t.val; break; } int i, dx, dy; for (i = 0; i < 4; ++i) { tmp = t; dx = tmp.x + dir[i][0]; dy = tmp.y + dir[i][1]; if (dx>=0 && dx<w && dy>=0 && dy<h && !vis[dy][dx] && map[dy][dx]!='#') { vis[dy][dx] = 1; tmp.x = dx; tmp.y = dy; if (map[dy][dx] == '.') tmp.val += 1; else tmp.val += d+1; pq.push(tmp); } } } } int main() { int t; scanf("%d", &t); while (t--) { node s; while (!pq.empty()) pq.pop(); memset(vis, 0, sizeof(vis)); ans = 0; scanf("%d %d %d", &h, &w, &d); int i, j; for (i = 0; i < h; ++i) { scanf("%s", map[i]); for (j = 0; j < w; ++j) { if (map[i][j] == 'S') { s.x = j; s.y = i; s.val = 0; pq.push(s); vis[i][j] = 1; } } } bfs(); printf("%d\n", ans+1); } return 0; }
相关文章推荐
- HDU 4198 Quick out of the Harbour(优先队列 + bfs)
- HDOJ 4198 Quick out of the Harbour(BFS)
- hdoj 4198 Quick out of the Harbour(bfs,优先队列)
- hrbust 1434 Quick out of the Harbour【Bfs+优先队列】
- HDU 4198 Quick out of the Harbour (优先队列BFS)
- HDU 4198 Quick out of the Harbour (bfs)
- HDU_4198_Quick out of the Harbour(优先队列BFS)
- hdu 4198 Quick out of the Harbour (bfs)
- hdoj 4198 Quick out of the Harbour
- HDU 4198 Quick out of the Harbour(BFS+优先队列)
- 杭电OJ 4198(广搜+优先队列~)图搜.Quick out of the Harbour
- hdu 4198 Quick out of the Harbour
- hdu 4198 Quick out of the Harbour(BFS+优先队列)
- hdu 4198 Quick out of the Harbour
- HDU - 4198 Quick out of the Harbour (BFS+优先队列)
- hdu 4198 Quick out of the Harbour
- HDU 4198 Quick out of the Harbour
- hdu 4198 Quick out of the Harbour
- hdu 【4198】Quick out of the Harbour
- HDU 4198 Quick out of the Harbour(优先队列)