我是靠谱客的博主 自然唇膏,最近开发中收集的这篇文章主要介绍HDU 1242 Rescue(广搜,优先队列) HDU 1242  Rescue,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

HDU 1242  Rescue

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


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
 

           广搜,遇到守卫,时间 +2 ,进入队列,因为需要在队列中重新排序,因此,要用到优先队列!

          这一题与 hdu 上面的 1180 诡异的楼梯 属于同一类型!

          附上hdu 1180 诡异楼梯 链接:http://blog.csdn.net/xia842655187/article/details/47361835

附上源代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
using namespace std;
int b[4][2] = {1,0,0,1,-1,0,0,-1}; // 下右左上
char Map[250][250];
int visit[250][250];
int n,m;
struct A
{
int x,y,step;
bool operator < (const A &a) const
{
return a.step < step;
}
};
int dfs(int x1,int y1,int x2,int y2)
{
int sx,sy,t;
priority_queue<A> Q;
A e;
e.x = x1;
e.y = y1;
e.step = 0;
Q.push(e);
visit[x1][y1] = 1;
while(!Q.empty())
{
e = Q.top();
if(e.x == x2 && e.y == y2)
break;
Q.pop();
for(int i = 0;i < 4;i++)
{
sx = e.x + b[i][0];
sy = e.y + b[i][1];
if(sx >= 0 && sx <= n && sy >= 0 & sy <= m && (Map[sx][sy] == '.' || Map[sx][sy] == 'x' || Map[sx][sy] == 'a') && !visit[sx][sy])
{
if(Map[sx][sy] == 'x')
{
t = e.step + 2;
}
else
t = e.step + 1;
A e1;
e1.x = sx;
e1.y = sy;
e1.step = t;
Q.push(e1);
visit[sx][sy] = 1;
}
}
}
if(Q.empty())
return -1;
else
{
while(!Q.empty())
Q.pop();
return e.step;
}
}
int main()
{
while(cin >> n >> m)
{
int x1,x2,y1,y2;
memset(Map,'',sizeof(Map));
memset(visit,0,sizeof(visit));
for(int i = 0;i < n;i++)
{
scanf("%s",Map[i]);
for(int j = 0;j < m;j++)
{
if(Map[i][j] == 'a')
{
x2 = i;
y2 = j;
}
if(Map[i][j] == 'r')
{
x1 = i;
y1= j;
}
}
}
int ans = dfs(x1,y1,x2,y2);
if(ans >= 0)
cout << ans << endl;
else
cout << "Poor ANGEL has to stay in the prison all his life." << endl;
}
return 0;
}


最后

以上就是自然唇膏为你收集整理的HDU 1242 Rescue(广搜,优先队列) HDU 1242  Rescue的全部内容,希望文章能够帮你解决HDU 1242 Rescue(广搜,优先队列) HDU 1242  Rescue所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部