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.)
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.
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是哨兵,只能向前后左右移动,移动一次时间加一,遇到哨兵要再加一个时间;
解题思路:优先队列加广搜;
感悟:广搜还是不大熟练,写着写着就不知道怎么写了,放假得好好写写!!;
代码:
#include
#include
#include
#include
#define maxn 205
using namespace std;
struct Fr
{
int
x,y,t;//分别表示朋友的坐标和走的步数;
bool
operator<(const Fr&other) const
{
return this->t>other.t;
}
};
int visit[maxn][maxn];//这个是记录步数的不是记录走没走的不能用布尔型
char mapn[maxn][maxn];
int n,m,direction[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int x1,y1,x2,y2;
int judge(int x,int y)
{
if(x<1||x>n||y<1||y>m||!visit[x][y]||mapn[x][y]=='#')
return 1;
else
return 0;
}
int bfs()
{
priority_queue Q;
Fr
fr,next_fr;
fr.x=x1;
fr.y=y1;
fr.t=0;
//printf("x1=%d x2=%d\n",x1,x2);
Q.push(fr);
visit[x1][y1]=0;
while(!Q.empty())
{
//printf("fr.x=%d x2=%d\n",fr.x,x2);
//printf("fr.y=%d y2=%d\n",fr.y,y2);
fr=Q.top();
Q.pop();
if(fr.x==x2&&fr.y==y2)
return fr.t;//终止条件
for(int i=0;i<4;i++)
{
next_fr=fr;
next_fr.x+=direction[i][0];
next_fr.y+=direction[i][1];
if(judge(next_fr.x,next_fr.y))
continue;
next_fr.t++;
if(mapn[next_fr.x][next_fr.y]=='x')
next_fr.t++;//有哨兵得再加一次;
if(visit[next_fr.x][next_fr.y]>=next_fr.t)
#include
#include
#include
#define maxn 205
using namespace std;
struct Fr
{
};
int visit[maxn][maxn];//这个是记录步数的不是记录走没走的不能用布尔型
char mapn[maxn][maxn];
int n,m,direction[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int x1,y1,x2,y2;
int judge(int x,int y)
{
}
int bfs()
{