zoukankan      html  css  js  c++  java
  • uva 11624(bfs)

    11624 - Fire!

    Time limit: 1.000 seconds

    Joe works in a maze. Unfortunately, portions of the maze have
    caught on re, and the owner of the maze neglected to create a re
    escape plan. Help Joe escape the maze.
    Given Joe's location in the maze and which squares of the maze
    are on re, you must determine whether Joe can exit the maze before
    the re reaches him, and how fast he can do it.
    Joe and the re each move one square per minute, vertically or
    horizontally (not diagonally). The re spreads all four directions
    from each square that is on re. Joe may exit the maze from any
    square that borders the edge of the maze. Neither Joe nor the re
    may enter a square that is occupied by a wall.
    Input
    The rst line of input contains a single integer, the number of test
    cases to follow. The rst line of each test case contains the two
    integers R and C, separated by spaces, with 1  R; C  1000. The
    following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
    C characters, and each of these characters is one of:
     #, a wall
     ., a passable square
     J, Joe's initial position in the maze, which is a passable square
     F, a square that is on re
    There will be exactly one J in each test case.
    Output
    For each test case, output a single line containing `IMPOSSIBLE' if Joe cannot exit the maze before the
    re reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
    Sample Input
    2
    4 4
    ####
    #JF#
    #..#
    #..#
    3 3
    ###
    #J.
    #.F
    Sample Output
    3
    IMPOSSIBLE

    简单bfs

    开始提议理解错了,wrong了好几次,才知道原来可以有多个火源地。

    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define ll long long
    #define _cle(m, a) memset(m, a, sizeof(m))
    #define repu(i, a, b) for(int i = a; i < b; i++)
    #define MAXN 1005
    char s[MAXN];
    int ma[MAXN][MAXN];
    bool vis[MAXN][MAXN];
    int d[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    bool flag;
    struct P
    {
        int x, y, s;
        P() : x(0), y(0), s(0) {}
        P(int x_, int y_, int s_) : x(x_), y(y_), s(s_) {}
    };
    P J, F;
    queue<P> q, Q;
    int r, c;
    
    int Judge(int x, int y)
    {
        if(x > 0 && y > 0 && x <= r && y <= c) return 1;
        return 0;
    }
    
    void init()
    {
        while(!q.empty()) q.pop();
        while(!Q.empty()) Q.pop();
        _cle(ma, 0);
        _cle(vis, false);
        flag = false;
    }
    
    int bfs()
    {
        vis[J.x][J.y] = 1;
        q.push(J);
        int last = 0;
        while(!q.empty())
        {
            P t = q.front();
            q.pop();
            if(t.x == 1 || t.y == 1 || t.x == r || t.y == c)
                return t.s + 1;
            if(flag && last != t.s + 1)
            {
                int siz = Q.size();
                while(siz--)
                {
                    P p = Q.front();
                    Q.pop();
                    repu(i, 0, 4)
                    {
                        int dx = p.x + d[i][0];
                        int dy = p.y + d[i][1];
                        if(Judge(dx, dy) && ma[dx][dy] == 1)
                        {
                            ma[dx][dy] = 2;
                            Q.push(P(dx, dy, t.s));
                        }
                    }
                }
                last = t.s + 1;
            }
            repu(i, 0, 4)
            {
                int dx = t.x + d[i][0];
                int dy = t.y + d[i][1];
                if(Judge(dx, dy) && !vis[dx][dy] && ma[dx][dy] == 1)
                {
                    vis[dx][dy] = 1;
                    q.push(P(dx, dy, t.s + 1));
                }
            }
        }
        return -1;
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            init();
            scanf("%d%d", &r, &c);
            repu(i, 0, r)
            {
                scanf("%s", s);
                repu(j, 0, c) if(s[j] == '.') ma[i + 1][j + 1] = 1;
                else if(s[j] == 'F')
                {
                    Q.push(P(i + 1, j + 1, 0));
                    flag = true;
                    ma[i + 1][j + 1] = 2;
                }
                else if(s[j] == 'J')
                {
                    J.x = i + 1, J.y = j + 1, J.s = 0;
                    ma[i + 1][j + 1] = 1;
                }
            }
            int re = bfs();
            if(re == -1) printf("IMPOSSIBLE");
            else printf("%d", re);
            puts("");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    KCF目标跟踪方法分析与总结
    C# 事件
    委托学习(3)
    委托学习(2)
    委托学习(1)
    本地无sqlserver服务下操作数据库 之GSQL
    unity Android 打包后读取 xml 文件
    Unity 3D 调用摄像头捕获照片 录像
    Unity 进度条3D制作(3D版)
    Unity 3D 进度条制作
  • 原文地址:https://www.cnblogs.com/sunus/p/4501929.html
Copyright © 2011-2022 走看看