题目网址: http://acm.hdu.edu.cn/showproblem.php?pid=1242
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)*
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
翻译:
题目描述:
天使被MOLIGPY抓住了!他被Moligpy监禁了。监狱被描述为N * M(N,M <= 200)矩阵。监狱里有WALL,ROAD和GUARD。
天使的朋友想要拯救天使。他们的任务是:接近天使。我们假设“接近天使”是为了达到安吉尔留下的位置。当网格中有一名后卫时,我们必须杀死他(或她?)才能进入网格。我们假设我们向上,向下,向右,向左移动需要1个单位时间,并且杀死一名守卫也需要1个单位时间。我们足够强大,可以杀死所有的守卫。
你必须计算接近天使的最短时间。(当然,我们只能将UP,DOWN,LEFT和RIGHT移动到绑定范围内的邻居网格。)
输入
第一行包含两个整数代表N和M.
然后是N行,每行有M个字符。“” 代表道路,“a”代表Angel,“r”代表Angel的每个朋友。
处理到文件的末尾。
输出
对于每个测试用例,您的程序应输出一个整数,代表所需的最短时间。如果这样的数字不存在,你应该输出一行包含“贫穷的ANGEL必须终生留在监狱里”。
题意: 本题是在一个图中解救天使的,和迷宫问题类似,不过这个里面有守卫,碰到守卫了要杀死,其中要花费1个单位时间。
思路: 因为要求求解救天使的最短时间,所以本题要用广搜来写;又因为有守卫,所以我们还要用到优先队列,然后在中间如果遇到守卫就加2个单位时间,否则能走的只加1个单位时间。
代码:
#include
#include
#include
using namespace std;
char a[201][201];
bool isv[201][201]; //记录访问过没有
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
int N,M;
int sx,sy,ex,ey;
struct NODE{
int x;
int y;
int step;
bool operator < (const NODE &n) const //自定义优先级。在优先队列中,优先级高的元素先出队列。
{
return n.step < step; //通过题意可知 step 小的优先级高,需要先出队。
}
};
bool judge(int x,int y)
{
if( x<1 || y<1 || x>N || y>M )
return 1;
if( isv[x][y] )
return 1;
if( a[x][y]=='#' )
return 1;
return 0;
}
int bfs() //返回从(x,y)开始广搜,到右下角的最短步数,如果无法到达右下角,返回0
{
memset(isv,0,sizeof(isv));
priority_queue <NODE> q; //定义一个优先队列
NODE cur,next;
cur.x = sx;
cur.y = sy;
cur.step = 0;
isv[sx][sy] = true;
q.push(cur); //第一个元素入队
while(!q.empty()){
cur = q.top(); //队首出队,注意不是front()
q.pop();
if(cur.x==ex && cur.y==ey) //到终点
return cur.step;
for(int i=0;i<4;i++){
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if( judge(nx,ny) ) //判定
continue;
//可以走
next.x = nx;
next.y = ny;
if(a[nx][ny]=='x')
next.step = cur.step + 2;
else
next.step = cur.step + 1;
isv[nx][ny] = true;
q.push(next);
}
}
return -1;
}
int main()
{
while(cin>>N>>M) //多组测试样例
{
for(int i=1;i<=N;i++) //输入
for(int j=1;j<=M;j++){
cin>>a[i][j];
if(a[i][j]=='a')
ex=i,ey=j;
else if(a[i][j]=='r')
sx=i,sy=j;
}
int step = bfs();
if(step==-1) //不能到达
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else
cout<<step<<endl;
}
return 0;
}
总结: 本题是多组测试样例,这个要注意哦。
广搜(BFS)总结 链接
杭电oj题目分类