HDU 4198 Quick out of the Harbour(优先队列 + bfs)
来源:互联网 发布:淘宝网 话费充值 编辑:程序博客网 时间:2024/05/19 07:07
解题思路:
直接的bfs,因为和时间有关,需要采用优先队列.
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <stack>using namespace std;const int MAXN = 500 + 10;char G[MAXN][MAXN];int N, M, D;struct Node{int x, y, time;bool operator < (const Node& rhs)const{return time > rhs.time;}};bool vis[MAXN][MAXN];int g[][2] = {{-1,0},{1,0},{0,-1},{0,1}};int bfs(int sx, int sy){memset(vis, false, sizeof(vis));priority_queue<Node> Q;while(!Q.empty()) Q.pop();vis[sx][sy] = 1;Node u, v;u.x = sx, u.y = sy, u.time = 1;Q.push(u);while(!Q.empty()){u = Q.top();Q.pop();for(int i=0;i<4;i++){int x = u.x + g[i][0];int y = u.y + g[i][1];if(x < 1 || x > N || y < 1 || y > M)return u.time;if(vis[x][y] || G[x][y] == '#')continue;if(G[x][y] == '@'){v.x = x, v.y = y, v.time = u.time + D + 1;vis[x][y] = true;Q.push(v);}if(G[x][y] == '.'){vis[x][y] = true;v.x = x, v.y = y, v.time = u.time + 1;Q.push(v);}}}return -1;}int main(){int T;scanf("%d", &T);while(T--){ int sx, sy;scanf("%d%d%d", &N, &M, &D);for(int i=1;i<=N;i++){for(int j=1;j<=M;j++){scanf(" %c", &G[i][j]);if(G[i][j] == 'S'){sx = i;sy = j;}}}/*for(int i=1;i<=N;i++){for(int j=1;j<=M;j++)cout<<G[i][j];cout<<endl;}*/int ans = bfs(sx, sy); printf("%d\n", ans);}return 0;}
0 0
- HDU 4198 Quick out of the Harbour (优先队列BFS)
- 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(优先队列 + bfs)
- HDU 4198 Quick out of the Harbour(BFS+优先队列)
- 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+优先队列】
- HDU4198 Quick out of the Harbour bfs+优先队列
- HDU 4198 Quick out of the Harbour (bfs)
- HDOJ 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
- hdu 4198 Quick out of the Harbour
- hdu 4198 Quick out of the Harbour
- SQL update select语句及应用
- reids客户端 redis-cli用法
- 压缩图片
- ORACLE 数据库名、实例名、ORACLE_SID的区别
- hdu 1102 uva 10397(最小生成树prim)
- HDU 4198 Quick out of the Harbour(优先队列 + bfs)
- Java 泛型小结
- 修改MYSQL提示符
- MongoDB学习 (五):查询操作符(Query Operators).1st
- Object.clone()方法
- 浏览器不同语言切换不同页面
- 怎么关闭指定端口 3306
- 【转载】Java之美[从菜鸟到高手演变]之设计模式二
- MySQL数据库表结构设计优化技巧总结