您的位置:首页 > 其它

专题二 1012

2016-04-24 12:49 381 查看

一. 题目编号

专题二 1012

Rescue

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

二. 简单题意

描述

给出一个n行m列的图,’#’代表墙,’.’代表守卫,’.’代表路,’a’代表angel,’r’代表angel的朋友。angel的朋友要接近angel,可以走’.’和’x’,每走一步需要1个单位的时间,当经过x所在的位置时,要杀死x才能继续往前走,但杀死x同样要消耗1个单位的时间。问从r到a最少需要多长时间;如果不能到达,输出”Poor ANGEL has to stay in the prison all his life.”。

重点

主人公的朋友可能有多个,这样我们必须从主人公出发去寻找他到所有朋友处的最小值,就是答案。

三. 解题思路形成过程

使用优先队列,每次从优先队列中取出步数最小的点,从这个点开始搜,直到找到a或者搜索结束。

如果不用优先队列,也可以在搜到一个x时,直接把这个点压入队列,不再往它的四个方向搜,能保证这个点能被再次搜到。

四. 感想

一定要读清题意!!!

不然错好几遍都不知道怎么错的,这不是单纯的简单BFS,得用BFS+优先队列才行

五. AC代码

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
struct node
{
int x, y;
int step;
};
queue<node> q;
int N, M, prove, sx, sy, visit[202][202];
char map[202][202];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1,};
int check(int x, int y)
{
if(x < 0 || x >= N || y < 0 || y >= M)
return 0;
else
return 1;
}
void BFS()
{
while(!q.empty())
q.pop();
node s, e;
memset(visit, 0, sizeof(visit));
s.step = 0;  s.x = sx;  s.y = sy;
q.push(s);
visit[s.x][s.y] = 1;
while(!q.empty())
{
s = q.front();
q.pop();
if(map[s.x][s.y] == 'a')
{
cout << s.step << endl;
prove =1;
}
//如取出的是在有警卫的格子中,即杀掉警卫再次进入队列
if(map[s.x][s.y] == 'x')
{
map[s.x][s.y] = '.';
s.step += 1;
q.push(s);
continue;
}
for(int i = 0; i < 4; i++)
{
e.x = s.x + dx[i];  e.y = s.y + dy[i];
if(!check(e.x, e.y) || visit[e.x][e.y]
|| map[e.x][e.y] == '#')
continue;
e.step = s.step + 1;
q.push(e);
visit[e.x][e.y] = 1;
}
}
}
int main()
{
while(cin >> N >> M)
{
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
{
cin >> map[i][j];
if(map[i][j] == 'r')
{ sx = i;  sy = j; }
}
prove = 0;
BFS();
if(!prove)
cout << "Poor ANGEL has to stay in the prison all his life." << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: