ZOJ 1649 && HDU 1242 Rescue (BFS + 優先隊列)

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


解析:

廣搜必須都是1個時間才能搜,才能保證這個BFS樹是等距離向外伸展的,而這個不是等距離的,所以需要一些處理。

1、我的方法是,找到天使後,把時間比下大小,最後輸出最小的。需要優化,只這麼做的話,會TLE的,如果走過一個格子,這個格子存走過時候的時間,下次再走到這個格子,如果時間比格子裏的短,就入隊,否則,就不用入隊了。20MS。

2. 優先隊列+BFS  因爲等距離的BFS的話,隊列裏的time值是從小往大排的,那直接用優先隊列就可以了丫  0MS


代碼及詳細解析如下:

方法 I :
/****ZOJ 1649****/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <set>
#include <queue>
#include <algorithm>
#define MAXN 210
#define INF 1000000    //不要太大,太大結果錯誤
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

struct
{
    int x, y;
    int step;
    int time;
}q[1000000];

char Map[MAXN][MAXN];     //地圖
int Min[MAXN][MAXN];      //走到每個位置所需的最小時間
int front, rear, res;
int n, m, Sx, Sy, Ex, Ey;
const int dx[] = {-1, 0, 1, 0};     //方向數組
const int dy[] = {0, 1, 0, -1};

bool check(int x, int y)    //走下一步的條件
{
    return x>=0 && y>=0 && x<n && y<m && Map[x][y]!='#';
}

void Init()
{
    front = rear = 0;
    memset(Min, INF, sizeof(Min));   //初始化Min
    Min[Sx][Sy] = 0;
    q[rear].x = Sx, q[rear].y = Sy;
    q[rear].time = 0, q[rear++].step = 0;  //將起始position加入隊列
}

int main(int argc, char *argv[])
{
    while(~scanf("%d %d", &n, &m)) {
        for(int i=0; i<n; i++) {
            scanf("%s", Map[i]);
            for(int j=0; Map[i][j]; j++) {
                if(Map[i][j] == 'a') { Ex=i, Ey=j; }     //記錄終點position
                else if(Map[i][j] == 'r') { Sx=i, Sy=j; }   //記錄起始position
            }
        }
        Init();      //個別數據初始化
        while(front < rear) {     //當隊列不爲空
            int px = q[front].x;
            int py = q[front].y;   //current position
            for(int i=0; i<4; i++) { 
                int xx = px + dx[i];
                int yy = py + dy[i];
                //printf("xx = %d  yy = %d\n", xx, yy);
                if(check(xx, yy)) {
                    //printf("Matched xx = %d, yy = %d\n", xx, yy);
                    //v[xx][yy] = 1;
                    int qt = q[front].time + 1;
                    if(Map[xx][yy] == 'x') qt++;   //如果是警衛,殺死,時間+1
                    if(qt < Min[xx][yy]) {        //如果這種走法比之前走到該位置所花的時間少,則加入隊列,否則不用加入隊列
                        Min[xx][yy] = qt;
                        q[rear].x = xx, q[rear].y = yy;
                        q[rear].step = q[front].step + 1;
                        q[rear++].time = qt;
                    }
                    //printf("Matched cur_char = %c\n", Map[px][py]);
                    //printf("Matched next_pos_char = %c\n", Map[xx][yy]);
                    //printf("Matched cur_step = %d, cur_time = %d\n", q[front].step, q[front].time);
                    //printf("Matched step = %d, time = %d\n", q[rear].step, q[rear].time);
                }
            }
            front++;
        }
        if(Min[Ex][Ey] < INF) printf("%d\n", Min[Ex][Ey]);
        else puts("Poor ANGEL has to stay in the prison all his life.");
    }
    return 0;
}


方法 II :
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <limits.h>
#include <set>
#include <queue>
#include <algorithm>
#define MAXN 210
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

typedef struct Node
{
    int x, y;
    int time;
}Node;

Node start;
priority_queue <Node> pq;
char Map[MAXN][MAXN];
int v[MAXN][MAXN], n, m, res;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

bool operator < (Node a, Node b)
{
    return a.time > b.time;
}

bool check(int x, int y)
{
    return x>=0 && y>=0 && x<n && y<m && Map[x][y]!='#' &&!v[x][y];
}

int BFS()
{
    Node cur;
    int xx, yy, px, py, T;
    while(!pq.empty()) {
        cur = pq.top(), pq.pop();
        px = cur.x, py = cur.y, T = cur.time;
        for(int i=0; i<4; i++) {
            xx = px + dx[i], yy = py + dy[i];
            if(check(xx, yy)) {
                v[xx][yy] = 1;
                if(Map[xx][yy] == 'a') return T+1;      //基友見面,返回
                cur.x = xx, cur.y = yy, cur.time = T+1;
                if(Map[xx][yy] == 'x') cur.time++;     //打死警衛,時間+1
                pq.push(cur);     //入隊
            }
        }
    }
    return -1;
}

int main(int argc, char *argv[])
{
    while(~scanf("%d %d", &n, &m)) {
        RST(v);
        while(!pq.empty()) pq.pop();   //清空隊列
        for(int i=0; i<n; i++) {
            scanf("%s", Map[i]);
            for(int j=0; j<m; j++) {
                if(Map[i][j] == 'r') {    //記錄初始position
                    start.x = i, start.y = j;
                    start.time = 0;
                    pq.push(start);    //入隊
                    v[i][j] = 1;
                }
            }
        }
        res = BFS();
        if(res != -1) printf("%d\n", res);
        else puts("Poor ANGEL has to stay in the prison all his life.");
    }
    return 0;
}


發佈了188 篇原創文章 · 獲贊 26 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章