Java 洛谷 P1789 【Mc生存】插火把

我的注释绝对到位
题目描述:
在这里插入图片描述
在这里插入图片描述题目链接:https://www.luogu.com.cn/problem/P1789

代码实例:


import java.util.Scanner;

public class Main{
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(); // n*n方阵
        int m = scanner.nextInt(); // m个火把
        int k = scanner.nextInt(); // k个萤石
        int a[][] = new int[103][103];
        int result = 0;
        n = n + 3; // 防止越界,提前将范围扩大,最后输出的时候再将范围缩小
        // 输入火把的坐标
        for (int i = 0; i < m; i++) {
    
    
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            x = x + 3; // 防止越界,提前将范围扩大,最后输出的时候再将范围缩小
            y = y + 3; // 防止越界,提前将范围扩大,最后输出的时候再将范围缩小

            // 中间m*m都亮起来
            for (int j = x - 2; j <= x; j++) {
    
    
                for (int r = y - 2; r <= y; r++) {
    
    
                    a[j][r] = 1;
                }
            }
            // 上中,下中,左中,右中,四个点亮起来
            a[x - 3][y - 1] = 1; // 上
            a[x + 1][y - 1] = 1; // 下
            a[x - 1][y - 3] = 1; // 左
            a[x - 1][y + 1] = 1; // 右

        }
        // 输入萤石的位置
        for (int i = 0; i < k; i++) {
    
    
            int x = scanner.nextInt();
            int y = scanner.nextInt();

            x = x + 3; // 防止越界,提前将范围扩大,最后输出的时候再将范围缩小
            y = y + 3; // 防止越界,提前将范围扩大,最后输出的时候再将范围缩小
            for (int j = x - 3; j <= x + 1; j++)
                for (int r = y - 3; r <= y + 1; r++)
                    a[j][r] = 1;
        }
        // 如果是0的就是怪, 防止越界,将之前放大的范围缩小输出
        for (int i = 3; i < n; i++) {
    
    
            for (int j = 3; j < n; j++) {
    
    
                //System.out.print(a[i][j] + " "); // 放开注释,输出最终的方阵
                if (a[i][j] == 0) {
    
    
                    result++;
                }
            }
            // System.out.println();// 放开注释,输出最终的方阵
        }
        System.out.println(result);
    }
}

测试结果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43290288/article/details/129109479
今日推荐