ZOJ 1649 Rescue(有敌人迷宫BFS)

题意 求迷宫中从a的位置到r的位置需要的最少时间  经过‘.‘方格需要1s  经过‘x’方格需要两秒  ‘#‘表示墙

由于有1s和2s两种情况  需要在基础迷宫bfs上加些判断

令到达每个点的时间初始为无穷大  当从一个点到达该点用的时间比他本来的时间小时  更新这个点的时间并将这个点入队  扫描完全图就得到答案咯

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int N = 205;
char mat[N][N];
int time[N][N], sx, sy;
int dx[4] = {0, 0, -1, 1};
int dy[4] = { -1, 1, 0, 0};
struct grid
{
    int x, y;
    grid(int xx = 0, int yy = 0): x(xx), y(yy) {}
};

void bfs()
{
    memset(time, 0x3f, sizeof(time));
    time[sx][sy] = 0;
    queue<grid> g;
    g.push(grid(sx, sy));

    while(!g.empty())
    {
        grid cur = g.front();
        g.pop();
        int cx = cur.x, cy = cur.y, ct = time[cx][cy];
        for(int i = 0; i < 4; ++i)
        {
            int nx = cx + dx[i], ny = cy + dy[i];
            if(mat[nx][ny] && mat[nx][ny] != '#')
            {
                int tt = ct + 1;
                if(mat[cx][cy] == 'x') ++tt;
                if(tt < time[nx][ny])
                {
                    time[nx][ny] = tt;
                    g.push(grid(nx, ny));
                }
            }
        }
    }
}

int main()
{
    int n, m, ex, ey;
    while (~scanf("%d%d", &n, &m))
    {
        memset(mat, 0, sizeof(mat));
        for(int i = 1; i <= n; ++i)
            scanf("%s", mat[i] + 1);
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= m; ++j)
                if(mat[i][j] == 'a') sx = i, sy = j;
                else if(mat[i][j] == 'r') ex = i, ey = j;
        bfs();
        if(time[ex][ey] != time[0][0])
            printf("%d\n", time[ex][ey]);
        else
            printf("Poor ANGEL has to stay in the prison all his life.\n");
    }

    return 0;
}
时间: 2024-10-23 19:33:07

ZOJ 1649 Rescue(有敌人迷宫BFS)的相关文章

zoj 1649 Rescue (bfs+队列)

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

zoj 1649 Rescue

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

ZOJ 1649 &amp;&amp; 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

ZOJ 1649 &amp;&amp; HDU 1242 Rescue

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

HDU 1242 &amp;&amp; ZOJ 1649( BFS (队列 || 优先队列)).

~~~~ 突然发现一篇搜索的题目都有写.昨天发现道bfs题目,HDU上AC, ZOJ上WA.不得不说HDU上的数据之水.. 今天早起刷题有了思路,并用队列和单调队列都写了一遍,0MS飘过~~ ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649 ~~~~ 首先有坑的地方是friends,对嘛,朋友有很多,ang

zoj 1649

//hnldyhy(303882171) 11:12:46// zoj 1649 //bfs +优先队列 #include <stdio.h>#include <iostream>#include <queue>using namespace std;struct node{ int x; int y; int step;}; bool operator<(const node &a,const node &b){ if (a.step>b.

ZOJ 1649:Rescue(BFS)

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

HDU1728-逃离迷宫-BFS

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27258    Accepted Submission(s): 6661 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越, 有些地

HDU 2102 A计划 (三维的迷宫BFS)

题目链接:pid=2102">传送门 题意: 三维的一个迷宫,起点在第一层的S(0,0,0)处,问是否能在规定的时间内走到第二层的P 处.'*'代表不能走,'.'代表能够走,'#'代表传送门,这里有一个trick,走到传送门的时 候必需要传送.之前没有注意到WA了非常多遍. 并且在初始的时候能够对地图进行一下处理,('*','#'),('#','*'),('#','#')这种肯定 是不能够走的,所以能够把他们都变成'*' 代码例如以下: #include <iostream>