HDU 2425 Hiking Trip【BFS+priority_queue】

本文介绍了一种结合BFS算法与优先级队列解决迷宫寻路问题的方法,通过合理安排不同地形的行走成本,寻找从起点到终点的最短时间路径。该方法适用于地图探索类问题,尤其是当需要考虑多种地形因素时。

 

 

Hiking Trip

 

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1444    Accepted Submission(s): 628

 

 

Problem Description

Hiking in the mountains is seldom an easy task for most people, as it is extremely easy to get lost during the trip. Recently Green has decided to go on a hiking trip. Unfortunately, half way through the trip, he gets extremely tired and so needs to find the path that will bring him to the destination with the least amount of time. Can you help him?
You've obtained the area Green's in as an R * C map. Each grid in the map can be one of the four types: tree, sand, path, and stone. All grids not containing stone are passable, and each time, when Green enters a grid of type X (where X can be tree, sand or path), he will spend time T(X). Furthermore, each time Green can only move up, down, left, or right, provided that the adjacent grid in that direction exists.
Given Green's current position and his destination, please determine the best path for him. 

 

 

Input

There are multiple test cases in the input file. Each test case starts with two integers R, C (2 <= R <= 20, 2 <= C <= 20), the number of rows / columns describing the area. The next line contains three integers, VP, VS, VT (1 <= VP <= 100, 1 <= VS <= 100, 1 <= VT <= 100), denoting the amount of time it requires to walk through the three types of area (path, sand, or tree). The following R lines describe the area. Each of the R lines contains exactly C characters, each character being one of the following: ‘T’, ‘.’, ‘#’, ‘@’, corresponding to grids of type tree, sand, path and stone. The final line contains four integers, SR, SC, TR, TC, (0 <= SR < R, 0 <= SC < C, 0 <= TR < R, 0 <= TC < C), representing your current position and your destination. It is guaranteed that Green's current position is reachable – that is to say, it won't be a '@' square.
There is a blank line after each test case. Input ends with End-of-File.

 

 

Output

For each test case, output one integer on one separate line, representing the minimum amount of time needed to complete the trip. If there is no way for Green to reach the destination, output -1 instead.

 

 

Sample Input


 

4 6 1 2 10 T...TT TTT### TT.@#T ..###@ 0 1 3 0 4 6 1 2 2 T...TT TTT### TT.@#T ..###@ 0 1 3 0 2 2 5 1 3 T@ @. 0 0 1 1

 

 

Sample Output


 

Case 1: 14 Case 2: 8 Case 3: -1

 

 

 

题意:

在地图里,有不同的环境而且花费的时间各不相同,遇到石头则不能通过,其他的路径可以通过,但花费时间不同,让你算出花费时间最少的那条路并输出花费的时间,这里如果用BFS+queue是没办法实现的,因为在广搜的时候,只能算出最短的步数step,开始的时候,我的思路就是DFS,DFS虽然能求出结果,但是,得出答案就必须要遍历整个地图,这无疑是会超时的,这里如果用到BFS+priority_queue的话,就能解决问题,这里字面描述清楚一些,代码要是有耐心的也可以看看,因为写得很乱,思路了解了就可以自己写写看,先说说普通的广搜,普通的广搜用到的队列queue,能求出步数最少到达目的地的值,因为每一步都是通过队列queue中的最小步数开始扩展的,比如平面中,假设4个方向上下左右都可以走,那么到达这四个方向花费的步数是上一步的步数+1,因为无需排序,队列中都是以步数最小的规则排序的,从而求得最后的最少步数到达目的地的值,那么我们可以做出这样的判断,如果要求花费时间最小,那么,我们是不是把队列中的数据按照花费时间最少为准排序,然后从这些最小的数据开始扩展就能求得最小花费时间的值了呢,实际上,是可以的,下面给出代码:

 

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<time.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<list>
#include<queue>
#include<map>

#define E exp(1)
#define PI acos(-1)
#define mod (ll)(1e9+9)
#define INF 0x3f3f3f3f;
#define MAX 40000
#define compare 0.00000001
#define exps 1e-8
#define fr_add(i,init,n) for(int i = init ; i < n ; ++i)
#define fr_div(i,init,n) for(int i = n ; i >= init ; --i)

//#define _CRT_SECURE_NO_WARNINGS
//#define LOCAL
using namespace std;

typedef long long ll;
typedef long double lb;

int vp, vs, vt, dir[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };
int n, m, Min, mark[22][22];
char mp[22][22];
struct point {
	int x, y, step;
	friend bool operator<(point p1, point p2) {
		return p1.step > p2.step;
	}
};
struct point start, End;
map<char, int> type;

int bfs(struct point tp) {

	priority_queue<struct point> qu;
	mark[tp.x][tp.y] = 1;
	qu.push(tp);
	struct point temp, Front;

	while (!qu.empty()) {
		Front = qu.top();
		qu.pop();

		if (Front.x == End.x&&Front.y == End.y) {
			int res = Front.step;
			return res;
		}
		fr_add(i, 0, 4) {

			if (Front.x + dir[i][0] < n && Front.x + dir[i][0] >= 0 &&
				Front.y + dir[i][1] < m && Front.y + dir[i][1] >= 0 &&
				mark[Front.x + dir[i][0]][Front.y + dir[i][1]] == 0 &&
				mp[Front.x + dir[i][0]][Front.y + dir[i][1]] != '@') {

				mark[Front.x + dir[i][0]][Front.y + dir[i][1]] = 1;
				temp.x = Front.x + dir[i][0]; temp.y = Front.y + dir[i][1];
				temp.step = Front.step + type[mp[Front.x + dir[i][0]][Front.y + dir[i][1]]];
				//	cout << temp.x << " " << temp.y << " " << temp.step << endl;
				qu.push(temp);
			}
		}
	}

	return -1;
}


int main(void)
{
#ifdef LOCAL
	freopen("data.in.txt", "r", stdin);
	freopen("data.out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false); cin.tie(0);
	int time = 0;
	while (scanf("%d%d", &n, &m) != EOF) {
		time++;
		scanf("%d%d%d", &vp, &vs, &vt);
		fr_add(i, 0, n) {
			scanf("%s", &mp[i]);
		}

		scanf("%d%d%d%d", &start.x, &start.y, &End.x, &End.y);
		memset(mark, 0, sizeof(mark));

		type['.'] = vs; type['#'] = vp; type['T'] = vt;
		start.step = 0;

		int res = bfs(start);

		printf("Case %d: %d\n", time, res);
	}
	//	end = clock();
	//	cout << "using tmie:" << (double)(end - start) / CLOCKS_PER_SEC * (1000) << "ms" << endl;
	//system("pause");
	return 0;
}

 

 

 

 

 

内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值