博客
关于我
Fire Game FZU - 2150
阅读量:526 次
发布时间:2019-03-08

本文共 2763 字,大约阅读时间需要 9 分钟。

为了解决这个问题,我们需要找到两个起点,使得从这两个起点同时点燃的火焰能够在最短的时间内烧掉所有的草。火焰可以水平、垂直相邻的四个方向扩散。我们需要计算这种情况下的最小时间,如果无法完全烧掉所有的草,则返回-1。

方法思路

  • 问题分析:火焰从两个点同时开始,四个方向扩散。我们需要找到最小的时间,使得所有的草都被烧掉。
  • 广度优先搜索(BFS):使用BFS来计算从两个起点同时开始的火焰扩散的时间。BFS适合处理这种层层扩散的问题。
  • 起点对遍历:遍历所有可能的起点对,包括同一个点。对于每个起点对,进行BFS,记录每个点的被烧时间,并检查是否覆盖了所有的草。如果有任何一个草未被烧到,则跳过这种起点对。
  • 最小时间计算:对于每个满足条件的起点对,记录最大的被烧时间,并找出所有起点对中的最小值。
  • 解决代码

    import sysfrom collections import dequedef solve():    T = int(sys.stdin.readline())    for case in range(1, T + 1):        N, M = map(int, sys.stdin.readline().split())        grid = []        for _ in range(N):            row = sys.stdin.readline().strip()            grid.append(row)        # 收集所有'#'的位置        cells = []        for i in range(N):            for j in range(M):                if grid[i][j] == '#':                    cells.append((i, j))        num_cells = len(cells)        if num_cells == 0:            print(-1)            continue        if num_cells == 1:            print(0)            continue        # 初始化最小时间为一个很大的值        min_time = float('inf')        # 遍历所有可能的起点对        for i1, j1 in cells:            for i2, j2 in cells:                # 创建一个距离矩阵,初始化为-1                distance = [[-1 for _ in range(M)] for _ in range(N)]                # 初始化队列,同时加入两个起点                queue = deque()                queue.append((i1, j1))                distance[i1][j1] = 0                queue.append((i2, j2))                distance[i2][j2] = 0                # BFS开始                while queue:                    x, y = queue.popleft()                    # 四个方向                    for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:                        nx = x + dx                        ny = y + dy                        if 0 <= nx < N and 0 <= ny < M:                            if distance[nx][ny] == -1 and grid[nx][ny] == '#':                                distance[nx][ny] = distance[x][y] + 1                                queue.append((nx, ny))                # 检查是否覆盖所有细胞                all_covered = True                max_dist = 0                for (a, b) in cells:                    if distance[a][b] == -1:                        all_covered = False                        break                    if distance[a][b] > max_dist:                        max_dist = distance[a][b]                if all_covered:                    if max_dist < min_time:                        min_time = max_dist        if min_time != float('inf'):            print(min_time)        else:            print(-1)if __name__ == "__main__":    solve()

    代码解释

  • 读取输入:读取输入的测试用例数和每个测试用例的网格。
  • 收集草的位置:遍历网格,收集所有草的位置。
  • 特殊情况处理:如果只有一个草,直接输出0。
  • 遍历起点对:对于每对可能的起点,进行BFS计算火焰扩散时间。
  • BFS处理:使用队列处理每个点的扩散,记录每个点的被烧时间。
  • 检查覆盖情况:检查是否覆盖了所有的草,如果覆盖,记录最大的被烧时间,并更新最小时间。
  • 输出结果:根据最小时间输出结果,否则输出-1。
  • 通过这种方法,我们可以高效地解决问题,确保在最短的时间内找到最优解。

    转载地址:http://emziz.baihongyu.com/

    你可能感兴趣的文章
    Mac book pro打开docker出现The data couldn’t be read because it is missing
    查看>>
    MAC M1大数据0-1成神篇-25 hadoop高可用搭建
    查看>>
    mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
    查看>>
    Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
    查看>>
    MangoDB4.0版本的安装与配置
    查看>>
    Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
    查看>>
    mapping文件目录生成修改
    查看>>
    MapReduce程序依赖的jar包
    查看>>
    mariadb multi-source replication(mariadb多主复制)
    查看>>
    MariaDB的简单使用
    查看>>
    MaterialForm对tab页进行隐藏
    查看>>
    Member var and Static var.
    查看>>
    memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
    查看>>
    memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
    查看>>
    Memcached:Node.js 高性能缓存解决方案
    查看>>
    memcache、redis原理对比
    查看>>
    memset初始化高维数组为-1/0
    查看>>
    Metasploit CGI网关接口渗透测试实战
    查看>>
    Metasploit Web服务器渗透测试实战
    查看>>
    MFC模态对话框和非模态对话框
    查看>>