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;
    }
    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    case when完成不同条件的显示
    联行号不正确的触发器
    |待研究|委托付款的支付状态触发器
    待解决:新增客商校验触发器|两个错误|
    C#.NET和C++结构体Socket通信与数据转换
    C#中struct和class的区别详解
    C#与C++数据类型比较及结构体转换[整理]
    surging+CentOS7+docker+rancher2.0 入门部署教程
    Google Maps API Key申请办法(最新)
    开源的api文档管理系统
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3095506.html
Copyright © 2011-2022 走看看