HDU 2425 Hiking Trip【BFS+priority_queue】

 

 

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;
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值