zoukankan      html  css  js  c++  java
  • UVA11624 Fire! BFS

                                11624 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
    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.
    #.FUniversidad de Valladolid OJ: 11624 – Fire! 2/2
    Sample Output
    3
    IMPOSSIBLE

    题意:

    Joe在一个农场工作,农场是一个n*m的矩阵,有一天,农场突然着火了。

    火每分钟会向4个方向扩散,还好wall烧不起来(但人也无法经过),Joe每分钟走一个单位的格子。

    当走出这个农场时就安全了。

    问Joe能走出农场吗?能的话,最少需要的时间。

    这个农场有一些草,墙壁,还有一个着火的位置,还有Joe。

    我的想法:

    -2代表wall,

    -1代表初始时的草

    0代表着火的位置

    -3代表Joe的位置

    struct Point 中的step表示经过点(x,y)时的时间。

    然后对0的位置进行BFS,若maze[i][j]==-1则会跟着燃烧,此时更新maze[i][j]的值,记录它燃烧的时间。

    再对Joe的位置进行BFS(我写了2个BFS),若Joe经过时的时间少于maze的值,即可经过。

    然后提交,wa了。

    想了一想,F(即0)的位置可能不止一个呢。

    修改,maze[i][j]=min(maze[i][j],du.step)

    tle了。

    再一想,若du.step>=maze[i][j](maze[i][j]!=-1时),说明到这个格子时间比前面的慢了,则不用继续BFS了,直接return  ;。

    改后575ms过了。

    点击展开代码
     
  • 相关阅读:
    网站备份list
    vnc checklist
    appnode iptables 规则后面覆盖前面的
    Appnode + Discuz checklist
    解决WORD文档无法显示链接的图像问题
    应用容器Application container
    要研究的内容
    转 Flex MXML编译成AS类
    Flex文件结构
    int a
  • 原文地址:https://www.cnblogs.com/-maybe/p/4379611.html
Copyright © 2011-2022 走看看