HDOJ 1242 Rescue (BFS + 优先队列)

来源:互联网 发布:云 大数据 广州 编辑:程序博客网 时间:2024/05/19 03:44

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242

一、题目描述

===========================================================================================================================

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+优先队列

典型的搜索题,BFS + 优先队列。不过,注意的是从终点(朋友所在位置)开始搜索,到起点(angel所在位置)结束,因为起点(朋友)有多个。

在此感谢博友@Eric_keke,从终点搜索的思想从他的博文中获取,点击打开链接

以下是能够AC的代码。

#include <iostream>#include <vector>#include <queue>#include <utility>#include <functional>using namespace std;const int INF = 0X3F3F3F3F;int n, m;int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};struct Point{    char ch;    int x, y;    int time;    bool operator>(const Point& p) const {        return time > p.time;    }} maze[200][200];int bfs(int ax, int ay) {    int mx, my;    priority_queue<Point, vector<Point>, greater<Point>> pq;    //maze[ax][ay].time = 0;    pq.push(maze[ax][ay]);    while(!pq.empty()) {        Point tmp = pq.top();        pq.pop();        if(tmp.ch == 'r') return tmp.time;        for(int i = 0; i < 4; ++i) {            mx = tmp.x + dir[i][0];            my = tmp.y + dir[i][1];            if(mx < 0 || mx >= n || my < 0 || my >= m || maze[mx][my].ch == '#') continue;            int mtime = tmp.time + 1;            if(maze[mx][my].ch == 'x') mtime += 1;            if(maze[mx][my].time > mtime) {                maze[mx][my].time = mtime;                pq.push(maze[mx][my]);            }        }    }    return 0;}int main() {    int ax = 0, ay = 0;    while(cin >> n >> m) {        for(int i = 0; i < n; ++i) {            for (int j = 0; j < m; ++j) {                cin >> maze[i][j].ch;                maze[i][j].x = i;                maze[i][j].y = j;                maze[i][j].time = INF;                if(maze[i][j].ch == 'a') {ax = i; ay = j; maze[i][j].time = 0;}            }        }        int ans = bfs(ax, ay);        if(ans > 0) cout << ans << endl;        else cout << "Poor ANGEL has to stay in the prison all his life." << endl;    }    return 0;}


三、仿BFS+普通队列(buggy)

然后,我又仿着BFS的搜索思路,用普通队列来实现。具体有几下几个改动:

1)普通队列代替优先队列,搜索在队列弹出所有元素后退出,期间已经访问过的房间可能重新加入到队列中。

2)比较从angel到各个朋友的时间,取最小值。

然后,此代码能通过样例,但不能通过所有case。目前,还不能举出不通过的反例。

欢迎讨论,此代码的bug到底在哪?

代码如下:

#include <iostream>#include <vector>#include <queue>#include <utility>#include <functional>using namespace std;const int INF = 0X3F3F3F3F;int n, m;int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};struct Point{    char ch;    int x, y;    int time;    bool operator>(const Point& p) const {        return time > p.time;    }} maze[200][200];void bfs(int ax, int ay) {    int mx, my;    //priority_queue<Point, vector<Point>, greater<Point>> pq;    //maze[ax][ay].time = 0;    queue<Point> pq;    pq.push(maze[ax][ay]);    while(!pq.empty()) {        //Point tmp = pq.top();        Point tmp = pq.front();        pq.pop();        //if(tmp.ch == 'r') return tmp.time;        for(int i = 0; i < 4; ++i) {            mx = tmp.x + dir[i][0];            my = tmp.y + dir[i][1];            if(mx < 0 || mx >= n || my < 0 || my >= m || maze[mx][my].ch == '#') continue;            int mtime = tmp.time + 1;            if(maze[mx][my].ch == 'x') mtime += 1;            if(maze[mx][my].time > mtime) {                maze[mx][my].time = mtime;                pq.push(maze[mx][my]);            }        }    }    //return 0;}int main() {    int ax = 0, ay = 0;    vector<pair<int, int>> vec;    while(cin >> n >> m) {        for(int i = 0; i < n; ++i) {            for (int j = 0; j < m; ++j) {                cin >> maze[i][j].ch;                maze[i][j].x = i;                maze[i][j].y = j;                maze[i][j].time = INF;                if(maze[i][j].ch == 'a') {ax = i; ay = j; maze[i][j].time = 0;}                if(maze[i][j].ch == 'r') {vec.push_back(make_pair(i,j));}            }        }//        int ans = bfs(ax, ay);//        if(ans > 0) cout << ans << endl;//        else cout << "Poor ANGEL has to stay in the prison all his life." << endl;        bfs(ax, ay);        int ans = INF;        for(auto &item : vec) {            if(maze[item.first][item.second].time < ans) ans =maze[item.first][item.second].time;        }        if(ans < INF) cout << ans << endl;        else cout << "Poor ANGEL has to stay in the prison all his life." << endl;    }    return 0;}


原创粉丝点击