hdoj1242(bfs+priority_queue)

之前用dfs剪枝AC了,http://www.cnblogs.com/ediszhao/p/4741825.html,这次用bfs+priority_queue来尝试解题

题意:拯救行动,天使r有多个朋友a(friends,在这里被坑了几次,没看清题意),天使被关在牢房里,等着朋友来拯救,求拯救天使的最短距离。

以天使为起点进行bfs,找到的a就是最小拯救时间值。

#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

struct node
{
    int x,y,cnt;
    friend bool operator < (node a,node b)
    {
        return a.cnt > b.cnt;
    }
};
const int M  = 205;
char map[M][M];
int visited[M][M];
int n,m;

int dire[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
priority_queue <struct node> q;
int bfs()
{
    node now,next;
    while (!q.empty())
    {
        now = q.top();
        q.pop();
        for (int i = 0; i< 4; i++)
        {
            int x = now.x+dire[i][0];
            int y = now.y+dire[i][1];
            if (x >= 0 && x < n && y >= 0 && y < m && map[x][y]!=‘#‘ && visited[x][y] == 0)
            {
                next.x = x;
                next.y = y;
                if (map[x][y] == ‘a‘)
                {
                    return (now.cnt+1);
                }
                if (map[x][y] == ‘x‘)
                {
                    next.cnt = now.cnt+2;
                }
                else next.cnt = now.cnt+1;
                visited[x][y] = 1;
                q.push(next);
            }
        }
    }
    return 0;
}
int main()
{
    while (cin >> n >> m)
    {
        node nn;
        while (!q.empty())    //一定要清空之前的队列,在这里wrong了
            q.pop();
        for (int i =0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> map[i][j];
                visited[i][j] = 0;
                if (map[i][j] == ‘r‘)
                {
                    nn.x = i;
                    nn.y = j;
                    nn.cnt = 0;
                    q.push(nn);
                    visited[i][j] = 1;
                }
            }
        }
        int res = bfs();
        if (!res)
            cout << "Poor ANGEL has to stay in the prison all his life.\n";
        else  cout << res << endl;
    }
    return 0;
}

/*
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
2 8
a.#####r
#..xxaxx
*/
时间: 2024-10-15 04:09:57

hdoj1242(bfs+priority_queue)的相关文章

hdu 1175 bfs+priority_queue

连连看 如上图所示如果采用传统bfs的话,如果按照逆时针方向从(1,1)-->(3,4)搜索,会优先选择走拐四次弯的路径导致ans错误: Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 34028    Accepted Submission(s): 8438 Problem Description "连连看"相信很多

HLG 1613 迷宫问题 (BFS + priority_queue)

链接: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1613 Description : 小z身处在一个迷宫中,小z每分钟可以走到上下左右四个方向的相邻格之一.迷宫中有一些墙和障碍物. 同时迷宫中也有一些传送门,当小z走到任意一个传送门时,可以选择传送到其他任意的传送门(传送是不花费时间的), 当然也可以停留在原地.现在小z想知道走出迷宫需要花费的最少时间. Input : 输入第一行为组

蛇与梯子游戏 (BFS + priority_queue)

"蛇和梯子"是一个在NXN的(0<N<=20)的方格棋盘上进行的游戏(如下图) 方格从1到N的平方编号.除了第1号和最后编号的方格,其它的格子都有可能有蛇或梯子存在(蛇和梯子的数量及具体位置由输入确定,它们的数量都在100之内并且蛇和梯子不能临近放置,也就是在任何了放置两者首尾的方格之间至少还有一个未放置任何东西的格子).开始的时候玩家把他们的标志物放在1号格子中.玩家轮流以扔骰子的方式移动他们的指示物.如果一个指示物到达了一条蛇的嘴部,则把它移回蛇的尾部.如果一个指示物到

poj 1324 Holedox Moving A*算法对bfs的优化

题意: 迷宫里有一条贪食蛇,求它的蛇头到迷宫左上角最少要多少步. 分析: 关键是将蛇的状态压缩编码,然后bfs,超时就改A*,这题有类似最短路径的性质,A*发现节点重复后不需要更新直接舍弃即可. 代码: //poj 1324 //sep9 #include <iostream> #include <algorithm> #include <queue> using namespace std; struct state { int x[10],y[10]; }; str

POJ 2312 Battle City(优先队列+BFS)

题目链接:http://poj.org/problem?id=2312 Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7085   Accepted: 2390 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often pl

HDU 1026 Ignatius and the Princess I(BFS+路径输出)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19800    Accepted Submission(s): 6452Special Judge Problem Description The Princess has been abducted by the BEelzebub

UVA 11573 - Ocean Currents(BFS+优先队列)

UVA 11573 - Ocean Currents 题目链接 题意:给定一个海面,数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量.每次询问给一个起点一个终点.问起点到终点耗费的最小能量 思路:广搜,队列用优先队列.每次取能量最低的点出来进行状态的转移 代码: #include <cstdio> #include <cstring> #include <queue> using namespace std; const int d[8][2] = {{-1

HDU 5094 --Maze【BFS &amp;&amp; 状态压缩】

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 903    Accepted Submission(s): 316 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

ZOJ 2655 Water Pipe bfs 带方向状态

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2655 Water Pipe Time Limit: 5 Seconds      Memory Limit: 32768 KB Two waterworks want to connect to each other with water pipes. Just as the map shows, the waterworks sit on two corners