zoukankan      html  css  js  c++  java
  • UVA 11624 Fire! (简单图论基础)

    Problem B: Fire!

    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 Specification

    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.

    Sample Input

    2
    4 4
    ####
    #JF#
    #..#
    #..#
    3 3
    ###
    #J.
    #.F
    

    Output Specification

    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.

    Output for Sample Input

    3
    IMPOSSIBLE
    

    Malcolm Sharpe, Ondřej Lhoták

    简单图论基础题:

    bfs

    //============================================================================
    // Name        : UVA11624.cpp
    // Author      : 
    // Version     :
    // Copyright   : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================
    
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    using namespace std;
    const int MAXN=1010;
    int n,m;
    char g[MAXN][MAXN];
    queue<pair<int,int> >q;
    int a[MAXN][MAXN];
    int move[][2]={{0,1},{0,-1},{1,0},{-1,0}};
    void bfs1()
    {
        memset(a,-1,sizeof(a));
        while(!q.empty())q.pop();
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(g[i][j]=='F')
                {
                    a[i][j]=0;
                    q.push(make_pair(i,j));
                }
        while(!q.empty())
        {
            pair<int,int> tmp=q.front();
            q.pop();
            int x=tmp.first;
            int y=tmp.second;
            for(int i=0;i<4;i++)
            {
                int tx=x+move[i][0];
                int ty=y+move[i][1];
                if(tx<0||ty<0||tx>=n||ty>=m)continue;
                if(a[tx][ty]!=-1)continue;
                if(g[tx][ty]=='#')continue;
                a[tx][ty]=a[x][y]+1;
                q.push(make_pair(tx,ty));
            }
        }
    }
    int b[MAXN][MAXN];
    int bfs2()
    {
        memset(b,-1,sizeof(b));
        while(!q.empty())q.pop();
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(g[i][j]=='J')
                {
                    q.push(make_pair(i,j));
                    b[i][j]=0;
                }
        while( !q.empty() )
        {
            pair<int,int> tmp=q.front();
            q.pop();
            int x=tmp.first;
            int y=tmp.second;
            if(x==0 || y==0 || x==n-1 || y==m-1)
                return b[x][y]+1;
            for(int i=0;i<4;i++)
            {
                int tx=x+move[i][0];
                int ty=y+move[i][1];
                if(tx<0||ty<0||tx>=n||ty>=m)continue;
                if(b[tx][ty]!=-1)continue;
                if(g[tx][ty]=='#')continue;
                if(a[tx][ty]!=-1 && b[x][y]+1>=a[tx][ty])continue;
                b[tx][ty]=b[x][y]+1;
                q.push(make_pair(tx,ty));
            }
        }
        return -1;
    }
    int main()
    {
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            for(int i=0;i<n;i++)
                scanf("%s",g[i]);
            bfs1();
            int ans=bfs2();
            if(ans==-1)printf("IMPOSSIBLE\n");
            else printf("%d\n",ans);
        }
        return 0;
    }
    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    读书是最划算的自我投资,免费送几本最好的Python学习电子书
    一个出身寒门的状元之编程
    幼儿园小朋友都在学人工智能了,已经从小学毕业的我们该如何学?
    python语音识别终极指南
    Python可视化神器——pyecharts的超详细使用指南!
    用 Python 分析过往 36 年春晚节目数据,发现一些趣事
    拉勾网数据分析职位分析
    按时分秒对数据进行分箱
    numpy
    基于协同过滤的推荐系统案列
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3095506.html
Copyright © 2011-2022 走看看