枫原万叶


私信TA

用户名:dotcpp0605256

访问量:237

签 名:

等  级
排  名 22493
经  验 615
参赛次数 0
文章发表 7
年  龄 0
在职情况 学生
学  校 怀化学院
专  业

  自我简介:

解题思路:

注意事项:

参考代码:

import java.util.Scanner;

public class 流感传染 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();  // 从用户输入中读取一个整数n,代表网格的边长
        sc.nextLine();  // 读取整数后的换行符,确保之后的nextLine能正确读取

        char[][] grid = new char[n][n];  // 初始化一个n*n的字符网格

        // 读取n行输入,填充到字符网格中
        for (int i = 0; i < n; i++) {
            String line = sc.nextLine();  // 读取一行输入
            for (int j = 0; j < n; j++) {
                grid[i][j] = line.charAt(j);  // 将字符填充到网格的相应位置
            }
        }

        int m = sc.nextInt();  // 从用户输入中读取一个整数m,代表经过的天数
        sc.close();  // 关闭扫描器,释放资源

        // 模拟m-1天的感染传播情况(因为最后一天的情况不需要再次传播)
        for (int day = 0; day < m-1; day++) {
            spreadInfection(grid, n);  // 调用spreadInfection函数进行感染传播
        }

        // 计算并输出第m天的感染人数
        int infectedCount = countInfected(grid, n);
        System.out.println(infectedCount);
    }

    // 感染传播函数
    private static void spreadInfection(char[][] grid, int n) {
        char[][] tempGrid = new char[n][n];  // 创建一个临时网格用于模拟感染传播后的情况

        // 初始化临时网格为当前网格的状态
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                tempGrid[i][j] = grid[i][j];
            }
        }

        // 遍历当前网格中的每个位置,如果是感染状态'@',则向其四周传播感染
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '@') {  // 当前位置是感染状态
                    spreadToNeighbors(tempGrid, i, j, n);  // 调用spreadToNeighbors函数向四周传播感染
                }
            }
        }

        // 更新原网格为临时网格的状态(即感染传播后的状态)
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                grid[i][j] = tempGrid[i][j];
            }
        }
    }

    // 向四周传播感染函数
    private static void spreadToNeighbors(char[][] grid, int row, int col, int n) {
        int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};  // 定义上、下、左、右四个方向

        // 遍历四个方向,尝试向每个方向传播感染
        for (int[] dir : directions) {
            int newRow = row + dir[0];  // 计算新位置的行坐标
            int newCol = col + dir[1];  // 计算新位置的列坐标

            // 检查新位置是否在网格范围内且为未感染状态'.',如果是则将其感染为'@'
            if (newRow >= 0 && newRow < n && newCol >= 0 && newCol < n && grid[newRow][newCol] == '.') {
                grid[newRow][newCol] = '@';  // 感染新位置
            }
        }
    }

    // 计算并返回网格中的感染人数(即'@'的个数)
    private static int countInfected(char[][] grid, int n) {
        int count = 0;  // 初始化感染人数为0
        // 遍历网格中的每个位置,统计感染人数
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '@') {  // 当前位置是感染状态
                    count++;  // 感染人数加1
                }
            }
        }
        return count;  // 返回感染人数
    }
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区