hdu Problem 1242 Rescue bfs + 优先队列

本文解析了一道典型的图遍历算法题目——救援Angel。题目要求玩家通过监狱地图找到并营救Angel,需考虑道路、墙壁及守卫等因素,并计算出最短营救时间。文章给出了两种实现方式的代码示例,一种考虑了多个朋友同时进行营救的情况。

问题地址

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24233    Accepted Submission(s): 8550


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: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
 

Input
First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. 

Process to the end of the file.
 

Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
 

Sample Input

  
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
 

Sample Output
这是刚写的, 因为题中说有多个朋友,所以原来的就不太对了

  
#include <bits/stdc++.h> using namespace std; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; bool vis[205][205]; char mp[205][205]; int ex, ey, n, m; struct node{     int x, y, step;     friend bool operator < (node a, node b) {         return a.step > b.step;     } }temp, a, b; int bfs(node x) {     priority_queue<node> que;     x.step = 0;     vis[x.x][x.y] = true;     while (!que.empty()) que.pop();     que.push(x);     while (que.size()) {         a = que.top();         que.pop();         //printf("%d %d %d\n", a.x, b.y, a.step);         if (a.x == ex && a.y == ey) return a.step;         for (int i = 0; i < 4; i++) {             b.x = a.x + dx[i];             b.y = a.y + dy[i];             b.step = a.step + 1;             if (b.x < 0 || b.x >= n || b.y < 0 || b.y >= m || mp[b.x][b.y] == '#')                 continue;             if (vis[b.x][b.y]) continue;             if (mp[b.x][b.y] == 'x') b.step++;             que.push(b);             vis[b.x][b.y] = true;         }     }     return -1; } int main() {     while (scanf("%d%d", &n, &m) != EOF) {         for (int i = 0; i < n; i++) {             scanf("%s", mp[i]);             for (int j = 0; j < m; j++) {                 if (mp[i][j] == 'a') {                     ex = i, ey = j;                   //  printf("%d %d\n", ex, ey);                 }             }         }         int ans = 1e9;         for (int i = 0; i < n; i++) {             for (int j = 0; j < m; j++) {                 if (mp[i][j] == 'r') {                     memset(vis, false, sizeof(vis));                     temp.x = i, temp.y = j;                     int t = bfs(temp);                     if (t != -1)                         ans = min(ans, t);                 }             }         }         if (ans != 1e9)              printf("%d\n", ans);         else printf("Poor ANGEL has to stay in the prison all his life.\n");     }     return 0; }
这是原来的:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#define MIN(a, b)	(a < b)? a: b
#define MAX_N 1000
using namespace std;
typedef pair<int, int> P;
const int INF = 99999;
char maze[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int w, h;
struct node {
    int x;
    int y;
    int step;
    friend bool operator < (node a,node b)
    {
        return a.step > b.step;
    }
}temp, Next;
int bfs(int x, int y ,int x2, int y2)
{
	memset(vis, false, sizeof(vis));
	priority_queue<node> que;
	if (!que.empty())	que.pop();
	temp.x = x, temp.y = y;
	temp.step = 0;
	que.push(temp);
	while (que.size()) {
		temp = que.top();
		que.pop();
		if (temp.x == x2 && temp.y == y2)	return temp.step;
		for (int i = 0; i < 4; i++) {
			Next.x = dx[i] + temp.x;
			Next.y = dy[i] + temp.y;
			if (Next.x >= h || Next.y >= w || Next.x < 0 || Next.y < 0 || maze[Next.x][Next.y] == '#' || vis[Next.x][Next.y])	continue;
			Next.step = temp.step + 1;
			vis[Next.x][Next.y] = true;
			if (maze[Next.x][Next.y] == 'x') {
				Next.step++;
			}
			que.push(Next);
		}
	}
	return -1;
}
int main()
{
	int sy, sx, ex, ey;
	while (scanf("%d %d", &h, &w) != EOF) {
		int ans;
		for (int i = 0; i < h; i++) {
			scanf("%s", &maze[i]);
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if(maze[i][j] == 'r')
                {
                    sx = i;
                    sy = j;
                }
                if(maze[i][j] == 'a')
                {
                    ex = i;
                    ey = j;
                }
			}
		}
		ans = bfs(sx, sy, ex, ey);
		if (ans == -1)	printf("Poor ANGEL has to stay in the prison all his life.\n");
		else	printf("%d\n", ans);
	}
	return 0;
}

转载于:https://www.cnblogs.com/cniwoq/p/6770961.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值