HDU - 4198 Quick out of the Harbour (BFS+优先队列)

Description

Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect and repair the ship. Now, a few days later, the pirates are getting landsick(Pirates get landsick when they don‘t get enough of the ships‘ rocking
motion. That‘s why pirates often try to simulate that motion by drinking rum.). Before all of the pirates become too sick to row the boat out of the harbour, captain Clearbeard decided to leave the harbour as quickly as possible.

Unfortunately the harbour isn‘t just a straight path to open sea. To protect the city from evil pirates, the entrance of the harbour is a kind of maze with drawbridges in it. Every bridge takes some time to open, so it could be faster to take a detour. Your
task is to help captain Clearbeard and the fastest way out to open sea.

The pirates will row as fast as one minute per grid cell on the map. The ship can move only horizontally or vertically on the map. Making a 90 degree turn does not take any extra time.

Input

The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:

1. One line with three integers, h, w (3 <= h;w <= 500), and d (0 <= d <= 50), the height and width of the map and the delay for opening a bridge.

2.h lines with w characters: the description of the map. The map is described using the following characters:

―"S", the starting position of the ship.

―".", water.

―"#", land.

―"@", a drawbridge.

Each harbour is completely surrounded with land, with exception of the single entrance.

Output

For every test case in the input, the output should contain one integer on a single line: the travelling time of the fastest route to open sea. There is always a route to open sea. Note that the open sea is not shown on the map, so
you need to move outside of the map to reach open sea.

Sample Input

2
6 5 7
#####
#S..#
#@#.#
#...#
#@###
#.###
4 5 3
#####
#S#.#
#@..#
###@#

Sample Output

16
11

Source

BAPC 2011

题意:求走出地图的最短时间,‘#‘不能走,‘.‘耗时一,‘@‘耗时K+1

思路:在BFS的基础上加上优先队列

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 510;

struct Point {
	int x, y, step;
	bool operator< (Point const &a) const {
		return step > a.step;
	}
} st, ed;
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int dx[4]={1, -1, 0, 0};
int dy[4]={0, 0, 1, -1};
int n, m, k;

void bfs() {
	memset(vis, 0, sizeof(vis));
	priority_queue<Point> q;
	vis[st.x][st.y] = 1;
	q.push(st);
	while (!q.empty()) {
		Point cur = q.top();
		q.pop();
		if (cur.x == 0 || cur.x == n-1 || cur.y == 0 || cur.y == m-1) {
			printf("%d\n", cur.step+1);
			return;
		}
		for (int i = 0; i < 4; i++) {
			int nx = cur.x + dx[i];
			int ny = cur.y + dy[i];
			if (vis[nx][ny] || map[nx][ny] == '#')
				continue;
			if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
				vis[nx][ny] = 1;
				Point tmp;
				if (map[nx][ny] == '.') {
					tmp.x = nx, tmp.y = ny;
					tmp.step = cur.step + 1;
				}
				else if (map[nx][ny] == '@') {
					tmp.x = nx, tmp.y = ny;
					tmp.step = cur.step + k + 1;
				}
				q.push(tmp);
			}
		}
	}
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d%d", &n, &m, &k);
		for (int i = 0; i < n; i++) {
			scanf("%s", map[i]);
			for (int j = 0; j < m; j++) {
				if (map[i][j] == 'S') {
					map[i][j] = '.';
					st.x = i, st.y = j, st.step = 0;
				}
			}
		}
		bfs();

	}
	return 0;
}

HDU - 4198 Quick out of the Harbour (BFS+优先队列)

时间: 2024-10-09 10:04:39

HDU - 4198 Quick out of the Harbour (BFS+优先队列)的相关文章

hdu 4198:Quick out of the Harbour解题报告

Quick out of the Harbour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1441    Accepted Submission(s): 575 Problem DescriptionCaptain Clearbeard decided to go to the harbour for a few days so

HDU 4198 Quick out of the Harbour(优先队列 + bfs)

解题思路: 直接的bfs,因为和时间有关,需要采用优先队列. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include

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 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 1242 找到朋友最短的时间 bfs+优先队列

找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.............. Sample Output13 bfs+优先队列 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 using names

[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:

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

[ACM] hdu 1253 胜利大逃亡 (三维BFS)

胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出

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 &