ZOJ 1649 Rescue

原题传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649

 

Rescue

 


 

Time Limit: 2 Seconds      Memory Limit: 65536 KB

 


 

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

 

 

分析:这道题用BFS可以解决,不过这里有坑~!在BFS时候,不能用queue队列,而应该用priority_queue优先队列,因为这里每条路径的每个点权值不一样,有x的权值是2,而‘.’的权值只有1,如果用queue找的紧紧是最短路径,而不是加权路径最短的那条,所以用优先队列来处理。

 

 

//图的BFS ZOJ 1649
#include <stdio.h>
#include <queue>
#include <string.h>

using namespace std;
const int MAXN = 210;
char list[MAXN][MAXN];
bool isVisited[MAXN][MAXN];
bool flag;

struct Step{
    int x;
    int y;
    int step;
    bool operator < (const Step &rhs) const{
        return rhs.step < step;
    }
};
int m,n,mins;

priority_queue<Step> pos;
Step first,curStep;
main(){
    while(scanf("%d%d",&m,&n)!= EOF){
        mins = 100000000;
        while(!pos.empty()){
            pos.pop();
        }
        flag = false;
        memset(isVisited,false,sizeof(isVisited));
        for(int i=1;i<=m;i++){
            scanf("%s",list[i]+1);
        }
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(list[i][j] == 'r'){
                    first.x = i;
                    first.y = j;
                    first.step = 0;
                }
            }
        }
        pos.push(first);
        isVisited[first.x][first.y] = true;
        while(!pos.empty()){
            curStep = pos.top();
            pos.pop();
            if(list[curStep.x][curStep.y] == 'a'){
               // printf("%d---%d",curStep.x,curStep.y);
                flag = true;
                mins = (mins>curStep.step)?curStep.step:mins;
            }
            if((list[curStep.x-1][curStep.y]=='.'||list[curStep.x-1][curStep.y]=='x'||
                list[curStep.x-1][curStep.y]=='a') && curStep.x-1>0 && !isVisited[curStep.x-1][curStep.y]){
                Step newStep;
                newStep.x = curStep.x-1;
                newStep.y = curStep.y;
                if(list[curStep.x-1][curStep.y]=='.'||list[curStep.x-1][curStep.y]=='a'){
                    newStep.step = curStep.step + 1;
                }else{
                    newStep.step = curStep.step + 2;
                }
                pos.push(newStep);
                isVisited[curStep.x-1][curStep.y] = true;
              //  printf("%d - %d\n",newStep.x,newStep.y);


            }
            if((list[curStep.x+1][curStep.y]=='.'||list[curStep.x+1][curStep.y]=='x'||
                list[curStep.x+1][curStep.y]=='a') && curStep.x+1<=m && !isVisited[curStep.x+1][curStep.y]){
                Step newStep;
                newStep.x = curStep.x+1;
                newStep.y = curStep.y;
                if(list[curStep.x+1][curStep.y]=='.'||list[curStep.x+1][curStep.y]=='a'){
                    newStep.step = curStep.step + 1;
                }else{
                    newStep.step = curStep.step + 2;
                }
                pos.push(newStep);
                isVisited[curStep.x+1][curStep.y] = true;
               // printf("%d - %d\n",newStep.x,newStep.y);
            }
            if((list[curStep.x][curStep.y-1]=='.'||list[curStep.x][curStep.y-1]=='x'||
                list[curStep.x][curStep.y-1]=='a') && curStep.y-1>0 && !isVisited[curStep.x][curStep.y-1]){
                Step newStep;
                newStep.x = curStep.x;
                newStep.y = curStep.y-1;

                if(list[curStep.x][curStep.y-1]=='.'||list[curStep.x][curStep.y-1]=='a'){
                    newStep.step = curStep.step + 1;
                }else{
                    newStep.step = curStep.step + 2;
                }
                pos.push(newStep);
                isVisited[curStep.x][curStep.y-1] = true;
               // printf("%d - %d\n",newStep.x,newStep.y);
            }
            if((list[curStep.x][curStep.y+1]=='.' ||list[curStep.x][curStep.y+1]=='x'||
                list[curStep.x][curStep.y+1]=='a' )&& curStep.y+1<=n && !isVisited[curStep.x][curStep.y+1]){
                Step newStep;
                newStep.x = curStep.x;
                newStep.y = curStep.y+1;
                if(list[curStep.x][curStep.y+1]=='.'||list[curStep.x][curStep.y+1]=='a'){
                    newStep.step = curStep.step + 1;
                }else{
                    newStep.step = curStep.step + 2;
                }
                pos.push(newStep);
                isVisited[curStep.x][curStep.y+1] = true;
               // printf("%d - %d\n",newStep.x,newStep.y);
            }
        }
        if(flag){
            printf("%d\n",mins);
        }else{
            printf("Poor ANGEL has to stay in the prison all his life.\n" );
        }
    }

}

 

 

你可能感兴趣的:(ZOJ 1649 Rescue)