• 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过了。

    点击展开代码
     
  • 相关阅读:
    关于89S51单片机数码管显示的小工具
    重新启动IIS的小工具
    有没有朋友做过动态表结构的,请教一下
    使用PPC的朋友慎用 星空极速3.2.070416_GD
    自定义app_offline.htm,让网站升级提示更专业
    2010全面兼容IE6/IE7/IE8/FF的CSS HACK写法
    CSS Sprites
    自适应宽度圆角按钮,导航,用到了滑动门技术
    设置frameset的高度
    jquey checkbox全选,反选
  • 原文地址:https://www.cnblogs.com/-maybe/p/4379611.html
Copyright © 2020-2023  润新知