(hdu step 4.2.3)Rescue(求从起点到终点的最少步数,遇到特殊节点需要耗时2秒)

题目:

Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 924 Accepted Submission(s): 387
 
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
13
 
Author
CHEN, Xue
 
Source
ZOJ Monthly, October 2003
 
Recommend
Eddy
 


题目分析:

                广搜。这道题需要使用优先队列来做。因为普通队列无法保证每次出对的是时间最小的元素。



代码如下:

/*
 * b.cpp
 *
 *  Created on: 2015年2月17日
 *      Author: Administrator
 */


#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;


const int maxn = 205;//最大的矩阵范围
const int inf = 999999;
char map[maxn][maxn];//原始地图
int vis[maxn][maxn];//用于保存到达某一节点的最小步数.如vis[i][j]=9.代表的是访问第i行第j列这个节点的最小步数是9步
int n,m;//行数,列数
int x1,y1;//起点的横坐标、纵坐标
int x2,y2;//终点的横坐标、纵坐标

int dir[4][2] = {//方向矩阵
		{-1,0},
		{1,0},
		{0,1},
		{0,-1}
};

//节点
struct Node{
	int x;//横坐标
	int y;//纵坐标
	int step;//到达这个节点的步数

	friend bool operator<(Node a,Node b){//在优先队列里面从小到大排序。
		return a.step > b.step;//....注意这种写法..
	}
};


/**
 * 判断下一步是否合法
 */
bool check(int x,int y){
	if(x < 0 || x >= n || y < 0 || y >= m){//判断是否越界
		return false;
	}

	if(map[x][y] == '#'){//判断是否是障碍物
		return false;
	}

	return true;//代表下一步合法..
}

/**
 * 广搜
 */
int bfs(){
	priority_queue<Node> q;

	Node root;//根节点的相应初始化
	root.x = x1;
	root.y = y1;
	root.step = 0;
	vis[x1][y1] = 0;

	Node k;

	q.push(root);//将根节点入队
	while(!q.empty()){//如果队列非空
		Node a = q.top();//取出队首元素
		q.pop();

		if(a.x == x2 && a.y == y2){//如果已经找到要找的点
			return a.step;//返回到达该点的最小步数
		}

		int i;
		for(i = 0 ; i < 4 ; ++i){//遍历寻找该节点的下一级元素
			k.x = a.x + dir[i][0];
			k.y = a.y + dir[i][1];

			if(check(k.x,k.y) == false){//判断下一级元素是否合法
				continue;
			}

			//如果下面能够执行,说明该节点合法

			k.step = a.step + 1;//计算到该节点的步数
			if(map[k.x][k.y] == 'x'){//如果遇到了卫兵
				k.step += 1;//需要的步数+1
			}
			if(vis[k.x][k.y] > k.step){//如果之前到达该点的步数>大于目前计算得到的到达该点的步数
				vis[k.x][k.y] = k.step;//更新到达该点的最小步数
				q.push(k);//将该元素进队
			}

		}
	}

	return 0;//返回这个表示没有路径到达终点
}

int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		int i;
		int j;
		for(i = 0 ; i < n ; ++i){
			scanf("%s",map[i]);//要注意这种写法。。。这道题用scanf("%c",&map[i][j])总会出这样的问题
			for(j = 0 ; j < m ; ++j){
				if(map[i][j] == 'a'){//标记终点
					x2 = i;
					y2 = j;
				}
				if(map[i][j] == 'r'){//标记起点
					x1 = i;
					y1 = j;
				}
			}
		}
		memset(vis,inf,sizeof(vis));//初始化到达每一点的最小步数


		int ans = bfs();
		if(ans == 0){//如果不能找到angle
			printf("Poor ANGEL has to stay in the prison all his life.\n");//输出不能找到的相应语句
		}else{//如果可以找到公主
			printf("%d\n",ans);//则输出找到公主所需要的最小步数
		}

	}

	return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值