题目描述:
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
3
题意描述:r代表起始点,a代表终点.代表路,.代表空地,#代表墙,x代表卫兵。.花费一秒时间,x花费两秒时间
问从起始点r到达终点a的最少时间。
分析:BFS进行广搜,主要处理走到x为两秒时间的问题,如果遇到x,首先把它变为.也就是空地,然后加入队列,花费的时间加1,在执行后面的步骤。具体见程序实现。
代码如下:
#include
#include
int n,m,ex,ey;
char map[205][205];//地图大小
int book[205][205];//标记数组
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//四个搜方向分别为左、下、右、上
struct node
{
int x;//横坐标
int y;//纵坐标
int f;//所用的时间
}q[43000];
int bfs(int bx,int by)
{
int tx,ty,k,tail,head;
book[bx][by]=1;//标记为1,表示走过
tail=head=0;//初始化
q[tail].x=bx;
q[tail].y=by;
q[tail].f=0;//刚开始所用时间为0
tail++;
while(head=n||ty<0||ty>=m||map[tx][ty]=='#')
continue;//边界条件
if(book[tx][ty]==0)
{//如果此地还没走过 ,则加入队列
book[tx][ty]=1;
q[tail].f=q[head].f+1;
q[tail].x=tx;
q[tail].y=ty;
tail++;
}
}
head++;//此处尤为重要,没有它无法进行下面的队列扩展了
}
return -1;
}
int main()
{
int i,j,bx,by,s;
while(scanf("%d%d",&n,&m) !=EOF)
{
for(i=0;i