HDU 1242 Rescue (广搜 + 优先队列)

13 篇文章 0 订阅
5 篇文章 0 订阅

HDU 1242


学会了优先队列的使用,就是要把用时最少的方案放在队列最前面。

关于对 < 的重载不是很懂,好像只要a > b 就是把小的放在队前,即最小堆,感觉好像和sort相反。。


参考博客:http://blog.csdn.net/cambridgeacm/article/details/7725146

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int N, M;
const int inf = 0x3f3f3f3f;
int map[210][210], vis[210][210];
int sx, sy;
int ans;
struct node {
	int x, y, time;
	friend bool operator < (node a, node b) {
		return a.time > b.time;
	}
};
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
void bfs() {
	priority_queue<node> q;
	node now, next;
	now.time = 0, now.x = sx, now.y = sy;
	q.push(now);
	vis[sx][sy] = 1;
	while(!q.empty()) {
		now = q.top();
		q.pop();
		if(map[now.x][now.y] == 'r') {
			ans = now.time;
			return;
		}
		int i;
		for(i = 0; i < 4; i++) {
			next.x = now.x + dir[i][0];
			next.y = now.y + dir[i][1];
			next.time = now.time;
			if(next.x >= 0 && next.x < N && next.y >= 0 && next.y < M && map[next.x][next.y] != '#' && vis[next.x][next.y] == 0) {
				vis[next.x][next.y] = 1;
				if(map[next.x][next.y] == 'x') next.time += 2;
				else next.time += 1;
				q.push(next);
			}
		}
	}
}
int main() {
	while(~scanf("%d %d", &N, &M)) {
		getchar();
		int i, j;
		for(i = 0; i < N; i++) {
			for(j = 0; j < M; j++) {
				scanf("%c", &map[i][j]);
				if(map[i][j] == 'a') {
					sx = i, sy = j;
				}
			}
			getchar();
		}
		memset(vis, 0, sizeof(vis));
		ans = inf;
		bfs();
		if(ans == inf) printf("Poor ANGEL has to stay in the prison all his life.\n");
		else printf("%d\n", ans);
	}
	
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值