zoukankan      html  css  js  c++  java
  • UVA 11624(两次bfs)

    UVA - 11624

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.
    Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.
    Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any
    square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.

    Input

    The first line of input contains a single integer, the number of test cases to follow. The first 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 fire
    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
    fire 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
    

    题意:有多个起火点,每个起火点可以往他四周烧,Joe在起火区域内,问Joe能不能从火场逃生。
    分析:两次bfs就可解决,第一个bfs计算烧到当前格需要多少时间,第二个bfs计算Joe能否逃生
    PS:注意是有多个起火点。。。

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 1010;
    const int inf = 0x3f3f3f3f;
    struct node{
        int x, y, s;
    };
    int n, m,fx, fy, jx, jy, ans;
    char s[N][N];
    bool vis[N][N];
    int e1[N][N], e2[N][N];
    int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    void bfs1(){
        memset(vis, false, sizeof(vis));
        queue<node> Q;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(s[i][j] == 'F'){
                    vis[i][j] = true;
                    Q.push((node){i, j, 0});
                }
            }
        }
        while(!Q.empty()){
            int xx = Q.front().x;
            int yy = Q.front().y;
            int ss = Q.front().s;
            Q.pop();
            for(int k = 0; k <= 3; k++){
                int tx = xx + Next[k][0];
                int ty = yy + Next[k][1];
                if(tx < 0 || ty < 0 || tx >= n || ty >= m || vis[tx][ty])
                    continue;
                if(s[tx][ty] == '.'){
                    vis[tx][ty] = true;
                    Q.push((node){tx, ty, ss + 1});
                    e1[tx][ty] = ss + 1;
                }
            }
        }
    }
    inline int bfs2(int x, int y){
        memset(vis, false, sizeof(vis));
        vis[x][y] = true;
    
        e2[x][y] = 0;
        queue<node> Q;
        Q.push((node){x, y, 0});
        while(!Q.empty()){
    
            int xx = Q.front().x;
            int yy = Q.front().y;
            int ss = Q.front().s;
            Q.pop();
            if(xx == 0 || yy == 0 || xx == n - 1 || yy == m - 1){
                return ss + 1;
            }
            for(int k = 0; k <= 3; k++){
                int tx = xx + Next[k][0];
                int ty = yy + Next[k][1];
                if(tx < 0 || ty < 0 || tx >= n || ty >= m || vis[tx][ty])
                    continue;
                if(s[tx][ty] == '.' && ss + 1 < e1[tx][ty]){
                    vis[tx][ty] = true;
                    Q.push((node){tx, ty, ss + 1});
                    e2[tx][ty] = ss + 1;
                }
            }
        }
        return  -1;
    }
    int main(){
        #ifdef ONLINE_JUDGE
        #else
            freopen("in.txt", "r", stdin);
        #endif // ONLINE_JUDGE
        int t;
        scanf("%d", &t);
        while(t--){
            memset(e1, inf, sizeof(e1));
            scanf("%d%d", &n, &m);
            for(int i = 0; i < n; i++){
                scanf("%s", s[i]);
            }
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    if(s[i][j] == 'J')
                        jx = i, jy = j;
                }
            }
            bfs1();
            int ans = bfs2(jx, jy);
            if(ans == -1)
                printf("IMPOSSIBLE
    ");
            else
                printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    Jmeter录制数据库脚本
    Jmeter代理方式录制并进行关联
    Jmeter基础知识
    总结Java各种API文档,提供下载.chm文件,HTML、java、w3school、servlet、js、spring、等等
    个人期末总结
    《软件工程导论》—领骑衫获奖感言
    201571030108/201571030112《小学四则运算练习软件软件需求说明》结对项目报告
    小学四则运算练习软件软件需求说明
    201571030108/201571030112《小学四则运算练习软件》结对项目报告
    小学生四则运算练习软件项目报告
  • 原文地址:https://www.cnblogs.com/kun-/p/10110770.html
Copyright © 2011-2022 走看看