UVA 11573 - Ocean Currents(BFS+优先队列)

UVA 11573 - Ocean Currents

题目链接

题意:给定一个海面,数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量。每次询问给一个起点一个终点。问起点到终点耗费的最小能量

思路:广搜,队列用优先队列。每次取能量最低的点出来进行状态的转移

代码:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int d[8][2] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
const int N = 1005;

int n, m, vis[N][N];
char g[N][N];

struct Node {
	int x, y, val;
	Node() {}
	Node(int x, int y, int val) {
		this->x = x;
		this->y = y;
		this->val = val;
	}
	bool operator < (const Node& c) const {
		return val > c.val;
	}
	void read() {
		scanf("%d%d", &x, &y);
	}
}s, e;

int bfs() {
	priority_queue<Node> Q;
	s.val = 0;
	Q.push(s);
	memset(vis, -1, sizeof(vis));
	vis[s.x][s.y] = 0;
	while (!Q.empty()) {
		Node u = Q.top();
		if (u.x == e.x && u.y == e.y) return u.val;
		Q.pop();
		for (int i = 0; i < 8; i++) {
			int xx = u.x + d[i][0];
			int yy = u.y + d[i][1];
			int val = u.val;
			if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
			if (i != g[u.x][u.y] - ‘0‘)
				val++;
			if (vis[xx][yy] == -1 || val < vis[xx][yy]) {
				vis[xx][yy] = val;
				Q.push(Node(xx, yy, val));
			}
		}
	}
}

int main() {
	while (~scanf("%d%d", &n, &m)) {
		for (int i = 1; i <= n; i++)
			scanf("%s", g[i] + 1);
		int q;
		scanf("%d", &q);
		while (q--) {
			s.read();
			e.read();
			printf("%d\n", bfs());
		}
	}
	return 0;
}
时间: 2024-12-27 08:31:27

UVA 11573 - Ocean Currents(BFS+优先队列)的相关文章

UVA 11573 - Ocean Currents【BFS+优先队列】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2620 题意:给定一个海面,数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量,每次询问给一个起点一个终点,问起点到终点耗费的最小能量 思路:广搜,队列用优先队列,每次取能量最低的点. 代码: #include <stdio.h> #include <

hdu 2757 Ocean Currents【广度优先搜索】

Ocean Currents Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1561    Accepted Submission(s): 516 Problem Description For a boat on a large body of water, strong currents can be dangerous, but

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

hdu 2102 A计划 详细题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 开始看到四分之一的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,不过在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间.果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,只不过多加了个参数,就是迷宫的层数,我用0代表第一层,1代表第二层,这在数组里面会体现的. struct node { int index;//层数

hdu 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an

poj 1724 ROADS (bfs+优先队列)

题目链接 题意:在有费用k限制的条件下,求从1到n的最短距离,如果最短距离相同求费用最小的,边为有向边,其中可能有 多个相同的源点和目标点,但是距离和费用不同. 分析:用bfs和邻接表来把每一个边搜一下,因为用了优先队列,所以先到n的一定是最小的 . 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio&