Hdoj 1424 Rescue 【BFS】

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18493    Accepted Submission(s): 6606


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
 

题意:给你一个图,‘#’代表墙, ‘.’代表路,‘x’代表是士兵。‘a’代表是终点,‘r’是起点,上下左右移动,每移动一步时间加1,遇到士兵要再加1,求需要的最少的

时间。

bfs+priority_queue

代码:

#include 
#include 
#include 
#include 
#include 

const int M = 205;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

using namespace std;

char map[M][M];
bool vis[M][M];
int n, m;
struct node{
    int step;
    int x, y;
    bool operator < (const node & a) const {
        return step > a.step;
    }
};
node st, en;

bool limit(node s){
    return (s.x >=0 && s.x < n&& s.y >= 0&& s.y < m&& map[s.x][s.y] != '#');
}

bool bfs(){
    memset(vis, 0, sizeof(vis));
    vis[st.x][st.y] = 1;
    priority_queueq;
    q.push(st);
    while(!q.empty()){
        node temp = q.top();
        q.pop();
        for(int i = 0; i < 4; ++ i){
            node cur = temp;
            cur.x += dx[i]; cur.y += dy[i];
            if(cur.x == en.x&& cur.y == en.y){
                printf("%d\n", cur.step+1); return 1;
            }
            if(!vis[cur.x][cur.y]&&limit(cur)){
                vis[cur.x][cur.y] = 1; 
                if(map[cur.x][cur.y] != '.'){
                    cur.step += 2;
                }
                else cur.step ++;
                q.push(cur);
            }
        }
    }
    return 0;
}

int main(){
    while(scanf("%d%d", &n, &m) == 2){
        for(int i = 0; i < n; ++ i){
            //cin >> map[i];
            scanf("%s", map[i]);
            for(int j = 0; j < m; ++ j)
            if(map[i][j] == 'a'){
                en.x = i; en.y = j;
            }
            else if(map[i][j] == 'r'){
                st.x = i; st.y = j; st.step = 0;
            }
        }
        bool flag = bfs();
        if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
} 


你可能感兴趣的:(BFS)