HDU4198-BFS+优先队列
来源:互联网 发布:淘宝走物流怎么发货 编辑:程序博客网 时间:2024/06/10 05:02
题目:题目链接
题意:给你一个地图,上面"."代表水,"#"表示陆地,"S"表示船的起始位置,"@"表示桥。我们知道船是不可以走陆地
的,所以只有"."和"@"可以走,但是走桥的时候有一个打开桥的时间d,过桥还需要1分钟,因为每个点可以走多次,所
以要用到优先队列,每次都让步数最小的点出队列结合BFS求最优!求出冲出地图的最小消费时间:
#include <iostream>#include <cstdio>#include <string>#include <string.h>#include <vector>#include <cstdlib>#include <algorithm>#include <cmath>#include <queue>#include <set>#include <stack>#include <functional>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cassert>#include <bitset>#include <stack>#include <ctime>#include <list>#define INF 0x7fffffff#define max3(a,b,c) (max(a,b)>c?max(a,b):c)#define min3(a,b,c) (min(a,b)<c?min(a,b):c)#define mem(a,b) memset(a,b,sizeof(a))using namespace std;int gcd(int n,int m){ if(n<m) swap(n,m); return n%m==0?m:gcd(m,n%m);}int lcm(int n,int m){ if(n<m) swap(n,m); return n/gcd(n,m)*m;}#define N 10000007int prime[N];void getPrime();void bash();void wzf();void SG();int QuickMod(int a, int b, int n);#define maxn 505struct node{ int x, y, time; friend bool operator < (node a, node b) { return a.time > b.time; }};priority_queue<node> q;char mp[maxn][maxn];char vis[maxn][maxn];int h, w, d;int sx, sy;int dir[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};int BFS(){ node now, next; while(!q.empty()) q.pop(); mem(vis, 0); now.x = sx; now.y = sy; now.time = 0; q.push(now); vis[sx][sy] = 1; while(!q.empty()) { now = q.top(); q.pop(); for(int i = 0; i < 4; ++i) { next.x = now.x + dir[i][0]; next.y = now.y + dir[i][1]; next.time = now.time+1; if(next.x < 0 || next.x >= h || next.y < 0 || next.y >= w) return next.time; if(mp[next.x][next.y] == '#' || vis[next.x][next.y]) continue; if(mp[next.x][next.y] == '@') { next.time += d; q.push(next); vis[next.x][next.y] = 1; } else if(mp[next.x][next.y] == '.') { q.push(next); vis[next.x][next.y] = 1; } } } return -1;}int main(){ int t; scanf("%d", &t); while(t--) { scanf("%d%d%d", &h, &w, &d); for(int i = 0; i < h; ++i) { scanf("%s", mp[i]); for(int j = 0; j < w; ++j) { if(mp[i][j] == 'S') { sx = i; sy = j; } } } int ans = BFS(); printf("%d\n", ans); } return 0;}int QuickMod(int a,int b,int n){ int r = 1; while(b) { if(b&1) r = (r*a)%n; a = (a*a)%n; b >>= 1; } return r;}void getPrime(){ memset(prime, 0, sizeof(prime)); prime[0] = 1; prime[1] = 1; for(int i = 2; i < N; ++i) { if(prime[i] == 0) { for(int j = i+i; j < N; j+=i) prime[j] = 1; } }}void bash(int n, int m){ if(n%(m+1) != 0) printf("1\n"); else printf("0\n");}void wzf(int n, int m){ if(n > m) swap(n, m); int k = m-n; int a = (k * (1.0 + sqrt(5.0))/2.0); if(a == n) printf("0\n"); else printf("1\n");}int numsg[N];void SG(int n){ int sum = 0; for(int i=0; i < n; i++) { scanf("%d",&numsg[i]); sum ^= numsg[i]; } if(sum == 0) printf("No\n"); else { printf("Yes\n"); for(int i = 0; i < n; i++) { int s = sum ^ numsg[i]; if(s < numsg[i]) printf("%d %d\n", numsg[i], s);//从有num[i]个石子的堆后剩余s个石子 } }}
- HDU4198-BFS+优先队列
- hdu4198-优先队列+bfs
- hdu4198(BFS+优先级队列)
- HDU4198 Quick out of the Harbour bfs+优先队列
- 优先队列+BFS
- hdu1242优先队列BFS
- acmdream1191 bfs+优先队列
- Hdu2822Dogs bfs+优先队列
- BFS+队列优先
- 优先队列+BFS
- hdu5040 优先队列+bfs
- HDU1026 bfs+优先队列
- hdu1071Nightmare(BFS+优先队列)
- HDU2026 BFS+优先队列
- HDOJ1026 优先队列bfs
- HDU1242 BFS+优先队列
- BFS-优先队列
- 优先队列+bfs 模板
- 移植glib库,procps到arm平台
- class和struct有什么区别?[转]
- 微信公众平台-消息接口指南
- AJAX实现防止Session过期 .
- Windchill 根据class文件名查找jar包
- HDU4198-BFS+优先队列
- Weka -- 聚类算法之K-means
- 每日一题(84) - 区间重合判断
- uva 10523
- 使用CTE转换string到Table
- ini文件操作
- iostream.h和iostream 区别
- 开博了
- servlet获取的页面参数自动填充到bean里