HDU - 2757 Ocean Currents (BFS+优先队列)
来源:互联网 发布:安卓必备软件 编辑:程序博客网 时间:2024/06/09 16:39
题意:求从开始点到目标点的最小的能量消耗,如果下一格的风向和当前的一样的话,消耗为0,否则为1,一共8个方向,其实方向是0
思路:BFS的基础上还要加上优先队列来取能量的最小值
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int MAXN = 1010;const int INF = 0x3f3f3f3f;struct node{int x,y,c;bool operator <(const node &a)const{return c > a.c;}}tmp;int n,m,sx,sy,ex,ey;char map[MAXN][MAXN];int f[MAXN][MAXN];int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};void bfs(){priority_queue<node> q;node a = {sx,sy,0};q.push(a);while (!q.empty()){tmp = q.top();q.pop();int tx = tmp.x,ty = tmp.y;if (tx == ex && ty == ey)return;int cost;for (int i = 0; i < 8; i++){tx = tmp.x + dir[i][0];ty = tmp.y + dir[i][1];if (tx >= 0 && tx < n && ty >= 0 && ty < m){int d = (map[tmp.x][tmp.y]-'0');if (i == d)cost = tmp.c;else cost = tmp.c + 1;node cur = {tx,ty,cost};if (f[tx][ty] > cost){f[tx][ty] = cost;q.push(cur);}}}}}int main(){while (scanf("%d%d",&n,&m) != EOF){for (int i = 0; i < n; i++)scanf("%s",map[i]);int t;scanf("%d",&t);while (t--){for (int i = 0; i <= n; i++)for (int j = 0; j <= m; j++)f[i][j] = INF;scanf("%d%d%d%d",&sx,&sy,&ex,&ey);sx--,sy--,ex--,ey--;f[sx][sy] = 0;bfs();printf("%d\n",f[ex][ey]);}}return 0;}
0 0
- hdu 2757 Ocean Currents(优先队列+bfs)
- HDU - 2757 Ocean Currents (BFS+优先队列)
- uva 11573 - Ocean Currents(bfs+优先队列)
- UVA 11573 Ocean Currents --BFS+优先队列
- UVA 11573 - Ocean Currents(BFS+优先队列)
- UVA 11573 - Ocean Currents【BFS+优先队列】
- hdu 2757 Ocean Currents【广度优先搜索】
- hdu 2757 Ocean Currents(BFS 比较简单的)
- Hdu 2757 Ocean Currents
- HDU 2757 Ocean Currents
- hdu 2757 Ocean Currents
- uva 11573 - Ocean Currents(bfs)
- hdu 2757 Ocean Currents (一搜到底)
- hdu(2575)Ocean Currents
- JOJ 2558 / HDU 2757 (Ocean Currents) 解题纠错
- hdu 2757 单调优先队列 bfs
- HDU 1026 (BFS + 优先队列)
- hdu 1242Rescue(bfs+优先队列)
- 每天一小步,成功一大步
- Python简单抓取原理引出分布式爬虫
- android导入
- JSP中的pagaEncoding和contentType中charset的区别
- ZJU-PAT 1057. Stack (30)---17有3组超时 浙大2013年上机复试第三题
- HDU - 2757 Ocean Currents (BFS+优先队列)
- 在 Sublime Text 2 中运行 PHP
- python 操作MySQL指南
- 去掉NetBeans在新建时的注释
- Cocos2d-x中常用的宏定义(一)
- 主成分分析(Principal components analysis)-最大方差解释
- 时间类
- 2014ACM集训13级PK赛5-Give Me the Number
- poj1581