AcWing4795.安全区域——学习笔记

目录

题目

代码一

思路一

 代码二

AC结果

 思路二:

一、获取数据(部分)

二、计算


题目

4795. 安全区域 - AcWing题库https://www.acwing.com/problem/content/4798/


代码一

import java.util.Scanner;

public class Main {
    public static long SIZE;
    public static void main(String[] args){
        //获取数据
        Scanner input = new Scanner(System.in);
        int size = input.nextInt();
        SIZE = size;
        int cars = input.nextInt();
        int[][] carPlace = new int[cars][2];//默认值为false

        for(int i = 0; i < cars; i++){
            carPlace[i][0] = input.nextInt();
            carPlace[i][1] = input.nextInt();
        }

        boolean[][] live = new boolean[size][size];
        int[] ans = new int[cars];
        for(int i = 0; i < cars; i++){
            live = putCar(carPlace[i][0], carPlace[i][1], live);
            ans[i] = calculateLive(live);
        }
        //输出
        for(int i = 0; i < cars; i++){
            System.out.print(ans[i] + " ");
        }

    }
    public static boolean[][] putCar(int x,int y,boolean[][] live){
        for(int i = 0; i < SIZE; i++){
            for(int j = 0; j < SIZE; j++){
                if (i + 1 == x || j + 1 == y){
                    live[i][j] = true;
                }
            }
        }
        return live;
    }
    public static int calculateLive(boolean[][] live){
        int count = 0;
        for(int i = 0; i < SIZE; i++){
            for(int j = 0; j < SIZE; j++){
                if(live[i][j] == false){
                    count++;
                }
            }
        }
        return count;
    }
}

思路一

一开始的思路与N皇后有点像,就是利用一个二维boolean棋盘记录某些行和某些列已经被攻击到了。然后遍历这个二维数组,把未被攻击的位置数出来,然后返回。这种方法只能通过一些数比较小的案例,遇到大的数据就会报超时。因为这份代码不能AC就不展开了。


 代码二

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        //获取数据
        Scanner input = new Scanner(System.in);
        long size = input.nextInt();
        int cars = input.nextInt();
        boolean[] line = new boolean[100010];
        boolean[] column = new boolean[100010];
        int l = 0;
        int c = 0;
        for(int i = 0; i < cars; i++){
            int x = input.nextInt();
            if (!line[x]){
                line[x] = true;
                l++;
            }
            int y = input.nextInt();
            if (!column[y]){
                column[y] = true;
                c++;
            }
            long ans = (size - l) * (size - c);
            System.out.print(ans + " ");
        }
    }
}

AC结果

 思路二:

其实大概思路也差不多,同样用boolean数组记录行和列“是否被攻击”。只不过从一个二维数组变为两个Boolean数组,大小为100010(题目规定数据:车的数量最多为100000)。然后每放一个车进去,“未被攻击”的区域可以直接用棋盘大小减去行或列已被攻击的数量直接计算出来。

一、获取数据(部分)

        //获取数据
        Scanner input = new Scanner(System.in);
        long size = input.nextInt();
        int cars = input.nextInt();
        boolean[] line = new boolean[100010];
        boolean[] column = new boolean[100010];
        int l = 0;
        int c = 0;

size表示棋盘的大小,cars表示车的数量。line表示每一行“是否被攻击”,column表示每一列“是否被攻击”,l表示已被攻击的行数,c表示已被攻击的列数。

二、计算

        for(int i = 0; i < cars; i++){
            int x = input.nextInt();
            if (!line[x]){
                line[x] = true;
                l++;
            }
            int y = input.nextInt();
            if (!column[y]){
                column[y] = true;
                c++;
            }
            long ans = (size - l) * (size - c);
            System.out.print(ans + " ");
        }

分别获取每一个车的坐标到x,y。然后比对各自的Boolean数组(line和column)判断这一行/列是否已经被攻击。若已被攻击则不需要处理,若未被攻击,则将该行/列的状态改为已被攻击(true),并且对应的l或者c数值自增。然后通过(size - l) * (size - c)计算未被攻击区域的大小。输出即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hokachi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值