zoukankan      html  css  js  c++  java
  • zoj 1649 Rescue

    BFS..第一次使用C++ STL的队列来写广搜。

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    const int maxn = 222;
    struct Point{ int time, x, y; };
    queue<Point> Q;
    char mapp[maxn][maxn];
    int mt[maxn][maxn];
    int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
    int main()
    {
        int n, m, i, j, sx, sy, ex, ey, ans;
        while (~scanf("%d%d", &n, &m))
        {
            for (i = 0; i < n; i++) scanf("%s", mapp[i]);
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < m; j++)
                {
                    mt[i][j] = 0x7FFFFFFF;
                    if (mapp[i][j] == 'a') sx = i, sy = j;
                    if (mapp[i][j] == 'r') ex = i, ey = j;
                }
            }    
            Point ss; ss.time = 0; ss.x = sx; ss.y = sy;
            Q.push(ss);
            ans = 0x7FFFFFFF;
            while (!Q.empty())
            {
                Point h, t;
                h = Q.front(); Q.pop();
                if (h.x == ex&&h.y == ey&&h.time < ans) ans = h.time;
                for (i = 0; i < 4; i++)
                {
                    int xx = h.x + dir[i][0], yy = h.y + dir[i][1];
                    if (xx >= 0 && xx <= n-1&&yy >= 0 && yy <= m-1)
                    {
                        if (mapp[xx][yy] != '#')
                        {
                            if (mapp[xx][yy] == 'x')
                            {
                                if (h.time + 2 < mt[xx][yy])
                                {
                                    t.time = h.time + 2;
                                    t.x = xx; t.y = yy;
                                    mt[xx][yy] = h.time + 2;
                                    Q.push(t);
                                }
                            }
                            else
                            {
                                if (h.time + 1 < mt[xx][yy])
                                {
                                    t.time = h.time + 1;
                                    t.x = xx; t.y = yy;
                                    mt[xx][yy] = h.time + 1;
                                    Q.push(t);
                                }
                            }
                        }
                    }
                }
            }
            if (ans != 0x7FFFFFFF)printf("%d
    ", ans);
            else printf("Poor ANGEL has to stay in the prison all his life.
    ");
        }
        return 0;
    }
  • 相关阅读:
    二维数组最大关联子数组
    四则运算(终极版)
    最大子数组
    四则运算(三) 记录日志
    四则运算(三)
    四则运算记录日志
    四则运算(二)
    简单web四则运算出题
    Daily Scrum
    Daily Scrum
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4467972.html
Copyright © 2011-2022 走看看