HDU 1242 Rescue (bfs + priority_queue)

    本來一直不想寫題解的,因爲網上有一大堆的題解,可能有更好的解法,但是最近在看之前做過的題目時,發現很難找到,然後準備開始把一些有收穫的題目記一下,哪怕是一點點的收穫也好、、

   

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
 
    一開始看到這題的時候,我以爲只是普通的bfs,興沖沖地敲完,以爲可以1A ,結果錯了,bug 也找不出來。
    看了discuss,可能是因爲數據比較弱,有種dir可以直接過的,但是其他的就不行了,這裏就需要用到優先隊列了,因爲是否要經過獄長會影響時間(題目中的符號爲“x”),
另外一點需要注意的是,題目中的r 可能存在多個,所以我們可以從a 開始搜。
    優先隊列的時候,注意重載<。

    優先隊列版、
   
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

char mpt[210][210];
bool vis[210][210];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
int n, m;

struct node
{
    int x, y;
    int step;
    bool operator <(const node &t) const  // 重載 <
    {
        return t.step < step;
    }
};

node sta;

void bfs()
{
    memset(vis, false, sizeof(vis));
    priority_queue<node> qe;
    while(!qe.empty())
    {
        qe.pop();
    }

    node tmp, next;

    qe.push(sta);
    vis[sta.x][sta.y] = true;
    while(!qe.empty())
    {
        tmp = qe.top(); // 注意這裏是top(),而普通隊列中是front()
        qe.pop();
        for(int i = 0; i < 4; i ++)
        {
            next.x = tmp.x + dir[i][0];
            next.y = tmp.y + dir[i][1];
            next.step = tmp.step + 1;
            if(mpt[next.x][next.y] == 'r')
            {
                printf("%d\n",next.step);
                return;
            }
            if(next.x > 0 && next.y > 0 && next.x < n && next.y < m && mpt[next.x][next.y] != '#' &&!vis[next.x][next.y])
            {
                if(mpt[next.x][next.y] == 'x')
                {
                    next.step ++;
                }
                vis[next.x][next.y] = true;
                qe.push(next);
            }
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.\n");
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 0; i < n; i ++)
        {
            scanf("%s",mpt[i]);
        }
        for(int i = 0; i < n; i ++)
        {
            for(int j = 0; j < m; j ++)
            {
                if(mpt[i][j] == 'a')
                {
                    sta.x = i;
                    sta.y = j;
                    sta.step = 0;
                    break;
                }
            }
        }
        bfs();
    }
}

    水過的普通bfs,據說是因爲數據太弱,然後過的。。
   
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

char mpt[210][210];
bool vis[210][210];
int dir[4][2] = {1,0,0,1,-1,0,0,-1}; // discuss 裏面說這個dir 的定義可以過,果然、、、
int n, m;

struct node
{
    int x, y;
    int step;
};

node sta;

void bfs()
{
    memset(vis, false, sizeof(vis));
    queue<node> qe;
    while(!qe.empty())
    {
        qe.pop();
    }

    node tmp, next;

    qe.push(sta);

    while(!qe.empty())
    {
        tmp = qe.front();
        qe.pop();
        for(int i = 0; i < 4; i ++)
        {
            next.x = tmp.x + dir[i][0];
            next.y = tmp.y + dir[i][1];
            next.step = tmp.step + 1;
            if(mpt[next.x][next.y] == 'r')
            {
                printf("%d\n",next.step);
                return;
            }
            if(next.x > 0 && next.y > 0 && next.x < n && next.y < m && mpt[next.x][next.y] != '#' &&!vis[next.x][next.y])
            {
                if(mpt[next.x][next.y] == 'x')
                {
                    next.step ++;
                }
                vis[next.x][next.y] = true;
                qe.push(next);
            }
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.\n");
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 0; i < n; i ++)
        {
            scanf("%s",mpt[i]);
        }
        for(int i = 0; i < n; i ++)
        {
            for(int j = 0; j < m; j ++)
            {
                if(mpt[i][j] == 'a')
                {
                    sta.x = i;
                    sta.y = j;
                    sta.step = 0;
                    break;
                }
            }
        }
        bfs();
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章