病毒感染时间(字节青训营)

复制代码
# 问题描述

在一个封闭的房间里摆满了座位,每个座位东西向和南北向都有固定 1 米的间隔。座位上坐满了人,坐着的人可能带了口罩,也可能没有带口罩。我们已经知道房间里的某个人已经感染了病毒,病毒的传播速度是每秒钟感染距离 1 米,但是超出 1 米病毒没有感染效力。病毒对于戴口罩的人需要两秒钟,或者一秒内被周围的两个人分别感染一次,才能被病毒感染。请实现一个算法,计算出来在给定的人员戴口罩情况,以及已经感染的人员位置情况下,病毒感染屋内所有人所需的时间。假定,已经感染的人戴和不戴口罩都具有相同的感染能力。

## 输入格式

第一行两个整数 n, m,表示座位有 n 行 m 列

接下来 n 行,每行 m 个整数 T(i, j)表示座位上的人戴口罩情况,0 表示未戴口罩,1 表示戴了口罩

最后一行两个整数 x, y 表示已经感染病毒的人所在座位

## 输出格式

输出一个整数表示病毒感染屋内所有人所需的时间

**输入样例**

4 4

0 1 1 1

1 0 1 0

1 1 1 1

0 0 0 1

2 2

**输出样例**

6

**数据范围**

座位横向和纵向最多 255
  • 二维布尔数组 :利用一个二维布尔数组存储房间内人员口罩情况(行,列),若带口罩为 true,不戴为 false

  • 感染情况记录 :再用一个二维布尔数组记录感染情况,感染为 true,根据 (x, y) 初始化感染人员位置。

  • 坐标变化定义 :定义上下左右的方法坐标变化,类似于(向左:x-1,y,向下:x,y-1)。

  • 时间初始化 :定义一个总时间 time = 0

  • bfs 函数定义

    • 若查询口罩情况为戴口罩,标记为不戴口罩,然后退出函数。
    • 查询感染情况,若没有感染,标记为已经感染,将这个位置加入 temp 队列,然后退出函数。
  • 主函数逻辑

    • 用一个键值对队列记录总已经感染的位置。
    • while 循环(直到队列长度等于 m*n 总座位的数量):
      • temp 队列(记录单次新增感染座位坐标),对于队列中所有感染的位置上下左右调用 bfs 函数。
      • 当次循环结束前 time += 1temp 队列元素加入总队列中。
      • 增加新位置的溢出判断和首位感染者位置是否合法的判断(若不合法,直接输出 0)。
java 复制代码
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static int[][] seats; // 存储口罩情况
    static boolean[][] infectionStatus; // 二维布尔数组存储感染情况
    static int[] dx = { -1, 1, 0, 0 }; // 左右下上的 x 坐标变化
    static int[] dy = { 0, 0, -1, 1 }; // 左右下上的 y 坐标变化

    public static void bfs(int x, int y, Queue<int[]> temp) {
        if (x < 0 || x >= seats.length || y < 0 || y >= seats[0].length) {
            return; // 新位置溢出判断
        }

        if (seats[x][y] == 1) {
            seats[x][y] = 0;
            return; // 如果有口罩,直接返回
        }
        if (!infectionStatus[x][y]) {
            infectionStatus[x][y] = true; // 标记为感染
            temp.offer(new int[] { x, y }); // 添加到临时队列
        }
    }

    public static int solution(int row_n, int column_m, int[][] seats, int[] patient) {
        if (patient[0] < 0 || patient[0] >= row_n || patient[1] < 0 || patient[1] >= column_m) {
            return 0; // 首位感染者位置不合法判断
        }
        Main.seats = seats;
        infectionStatus = new boolean[row_n][column_m];
        int x = patient[0];
        int y = patient[1];
        infectionStatus[x][y] = true;

        Queue<int[]> totalInfected = new LinkedList<>();
        totalInfected.offer(new int[] { x, y }); // 添加到临时队列
        Queue<int[]> temp = new LinkedList<>();
        int time = 0;

        while (totalInfected.size() < row_n * column_m) {
            // System.out.println("时间: " + time + ", 当前总感染位置数: " + totalInfected.size()); //
            // 调试打印
            for (int[] position : totalInfected) {
                int curX = position[0];
                int curY = position[1];
                for (int i = 0; i < 4; i++) {
                    int newX = curX + dx[i];
                    int newY = curY + dy[i];
                    if (newX >= 0 && newX < row_n && newY >= 0 && newY < column_m) {
                        bfs(newX, newY, temp);
                    }
                }
            }
            time++;
            totalInfected.addAll(temp);
            // System.out.println("本轮新增感染位置: " + temp.size()); // 调试打印
            temp.clear();
        }
        return time;
    }

    public static void main(String[] args) {
        // You can add more test cases here
        int[][] testSeats1 = { { 0, 1, 1, 1 }, { 1, 0, 1, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 1 } };
        int[][] testSeats2 = { { 0, 1, 1, 1 }, { 1, 0, 1, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 1 } };
        int[][] testSeats3 = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
        int[][] testSeats4 = { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } };
        int[][] testSeats5 = { { 1 } };

        System.out.println(solution(4, 4, testSeats1, new int[] { 2, 2 }) == 6);
        System.out.println(solution(4, 4, testSeats2, new int[] { 2, 5 }) == 0);
        System.out.println(solution(4, 4, testSeats3, new int[] { 2, 2 }) == 4);
        System.out.println(solution(4, 4, testSeats4, new int[] { 2, 2 }) == 6);
        System.out.println(solution(1, 1, testSeats5, new int[] { 0, 0 }) == 0);
    }
}
相关推荐
互联网全栈架构1 小时前
遨游Spring AI:第一盘菜Hello World
java·人工智能·后端·spring
qq_429879671 小时前
省略号和可变参数模板
开发语言·c++·算法
优秀的颜2 小时前
计算机基础知识(第五篇)
java·开发语言·分布式
BillKu2 小时前
Java严格模式withResolverStyle解析日期错误及解决方案
java
飞川撸码2 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
网安INF2 小时前
ElGamal加密算法:离散对数难题的安全基石
java·网络安全·密码学
Neil今天也要学习2 小时前
永磁同步电机参数辨识算法--IPMSM拓展卡尔曼滤波全参数辨识
单片机·嵌入式硬件·算法
yzx9910133 小时前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
AWS官方合作商3 小时前
在CSDN发布AWS Proton解决方案:实现云原生应用的标准化部署
java·云原生·aws
亮亮爱刷题3 小时前
算法练习-回溯
算法