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 <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;
}
时间: 2024-10-30 04:50:10

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

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 mo

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 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间. 比较麻烦的是需要记录路径.还要记录是在走路还是在打怪. 因为求最短路,所以可以使用bfs. 因为进过每一个点花费时间不同,所以可以使用优先队列. 因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点.当然,记录方法不止一种. 上代码—— 1 #include <c

hdu 4006 The kth great number (优先队列+STB+最小堆)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6637    Accepted Submission(s): 2671 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

HDU 1874-畅通工程续(Dijkstra+优先队列)

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28578    Accepted Submission(s): 10382 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行

HDU 5349 MZL&#39;s simple problem(优先队列)

MZL's simple problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 776    Accepted Submission(s): 375 Problem Description A simple problem Problem Description You have a multiple set,and now

hdu 1026 Ignatius and the Princess I (BFS+优先队列)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11700    Accepted Submission(s): 3653Special Judge Problem Description The Princess has been abducted by the BEelzebub

Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14624    Accepted Submission(s): 4634 Special Judge Problem Description The Princess has been abducted by the BEelzeb