HDU 1242 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
 

Author
CHEN, Xue
题意:天使朋友要在牢狱里解救天使,如果遇到守卫,则杀死守卫(需要花费一个step的时间),其中天使的朋友不一定只有一个。a代表天使,r代表天使的朋友。求最短需要多少step找到天使。
思路:因为天使的朋友不一定只有一个,所以可以用天使来找他的朋友。简单明了。难点在于遇到守卫的处理方式问题,单纯的只将step+1是不对的。毕竟广搜是一个个分身问题,如果遇到守卫,只需重新加入一次队列就ok了。。
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std; int n,m;
#define N 1000
char map[N][N]; int flog; int dir[4][2]={0,1,0,-1,1,0,-1,0}; //定义4个方向。 struct node { int x; int y; int step; }; struct node e,s; void bfs(int x,int y) {
    queue<node>q;
    s.x=x;
    s.y=y;
    s.step=0;
    q.push(s);
    map[s.x][s.y]='#'; while(!q.empty()) {
        s=q.front();
        q.pop(); if(map[s.x][s.y]=='r') //如果找到天使的朋友。则输出step,因为是同时分身,所以第一次找到天使的朋友花的步数可定时最小的。 {
            printf("%d\n",s.step);
            flog=1; break; } if(map[s.x][s.y]=='@'                    //引入一个新的字符,@,表示重新入队。step++; {
            map[s.x][s.y]='#';
            s.step++;
            q.push(s); continue; } int i; for(i=0;i<4;i++) {
        e.x=s.x+dir[i][0];
        e.y=s.y+dir[i][1]; if(e.x>=1&&e.x<=n&&e.y>=1&&e.y<=m&&map[e.x][e.y]!='#') //判断边界和位置信息 { if(map[e.x][e.y]=='r') //如果遇到朋友。加入队列 { 
                e.step=s.step+1;
                q.push(e); } if(map[e.x][e.y]=='.') //可走的话。 {
                map[e.x][e.y]='#';
                e.step=s.step+1;
                q.push(e); }  if(map[e.x][e.y]=='x') //遇到守卫,则将x变为@,重新入队一次就行 了, {
                map[e.x][e.y]='@';
                e.step=s.step+1;
                q.push(e); } } } } }0;
 int i; for(i=1;i<=n;i++) {
        scanf("%s",map[i]+1); } int j; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(map[i][j]=='a') {
                map[i][j]='.'; //找出初始位置。
                tx=i;
                ty=j; } } }
    bfs(tx,ty); if(flog==0) {
        printf("Poor ANGEL has to stay in the prison all his life.\n"); } } return 0; }
        
int main(){ int tx,ty; while(scanf("%d%d",&n,&m)!=EOF) { flog=

你可能感兴趣的:(bfs)