博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-1242 Rescue BFS+优先队列
阅读量:7191 次
发布时间:2019-06-29

本文共 3107 字,大约阅读时间需要 10 分钟。

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5895    Accepted Submission(s): 2192

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
 
  一个简单的搜索题,这里因为题目中说了可能有多个营救者,所以从a开始反向搜索,这样便能找到最近的营救者,该题在入队时因为时间并不是严格的+1,所以在一定程度上破坏了BFS的性质,所以加之优先队列进行修正。
  代码如下:
#include 
#include
#include
#include
using namespace std;char map[205][205], hash[205][205];struct Node{ int x, y, step; bool operator < ( const Node &t ) const { return t.step< step; }}info;int sx, sy, dir[4][2]= { 1, 0, -1, 0, 0, 1, 0, -1 };bool BFS( int &ans ){ memset( hash, 0, sizeof( hash ) ); priority_queue< Node >q; info.x= sx, info.y= sy, info.step= 0; hash[sx][sy]= 1; q.push( info ); while( !q.empty() ) { Node pos= q.top(); q.pop(); if( map[ pos.x ][ pos.y ]== 'r' ) { ans= pos.step; return true; } for( int i= 0; i< 4; ++i ) { int x= pos.x+ dir[i][0], y= pos.y+ dir[i][1], step= pos.step+ 1; if( map[x][y]!= '#'&& map[x][y]!= 0 ) { if( ( map[x][y]== '.'|| map[x][y]== 'r' )&& !hash[x][y] ) { info.x= x, info.y= y, info.step= step; hash[x][y]= 1; q.push( info ); } else if( map[x][y]== 'x'&& !hash[x][y] ) { info.x= x, info.y= y, info.step= step+ 1; hash[x][y]= 1; q.push( info ); } } } } return false;}int main(){ int N, M; while( scanf( "%d %d", &N, &M )!= EOF ) { int flag= 0; memset( map, 0, sizeof( map ) ); for( int i= 1; i<= N; ++i ) { scanf( "%s", map[i]+ 1 ); for( int j= 1; j<= M; ++j ) { if( !flag&& map[i][j]== 'a' ) { sx= i, sy= j; flag= 1; } } } int ans; if( BFS( ans ) ) { printf( "%d\n", ans ); } else { puts( "Poor ANGEL has to stay in the prison all his life." ); } }}

转载于:https://www.cnblogs.com/Lyush/archive/2011/08/09/2132087.html

你可能感兴趣的文章
聊下并发和Tomcat线程数(错误更正)
查看>>
L2-001. 紧急救援(迪杰斯特拉算法)
查看>>
leetcode297. 二叉树的序列化与反序列化
查看>>
bzoj3272 3638
查看>>
bzoj3192
查看>>
Controlled Tournament(状态压缩DP)
查看>>
Mac下,如何把项目托管到github
查看>>
记微软OpenHack机器学习挑战赛
查看>>
new XSSFWorkbook(is); Package should contain a content type part [M1.13]
查看>>
MongoDB安全及身份认证
查看>>
oc精简笔记
查看>>
python的多线程和守护线程
查看>>
traditional:true
查看>>
PS字体加粗的小方法、、
查看>>
构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root
查看>>
友元程序集
查看>>
Mysql表编辑
查看>>
规定密码以字母开头只能包含字母、数字和下划线
查看>>
计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task
查看>>
maven下载及安装
查看>>