202206-2 寻宝!大冒险!(java)

文章介绍了一种优化的寻宝算法,通过构造绿化树副本,减少复杂度,快速找出宝藏地点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

202206-2 寻宝!大冒险!

题目描述

在这里插入图片描述

题解

由于n<=1000,S<=50,L<10^9 如果直接模拟整个地图的话肯定是不太行,题目中说了,藏宝图的左下角必为一颗树,所以可以以每棵树为原点,生成一个与藏宝图大小相同的绿化树副本,然后判断每个点的值是否相等,这么做的话时间复杂度是N*S^2 ,大概是10^5,不会超时,两个二维数组也很小,所以直接开干

代码

import java.util.Scanner;

    public class Main {
        static int[][] trees = new int[1024][2];
        static int[][] trasurea = new int[52][52];
        static int[][] temp = new int[52][52];
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int L = sc.nextInt();
            int S = sc.nextInt();

            for (int i = 0; i < n; i++) {
                int x = sc.nextInt(), y = sc.nextInt();
                    trees[i] = new int[]{x,y};
            }
            for (int i = S; i >= 0; i--) {
                for (int j = 0; j <= S; j++) {
                    trasurea[i][j] = sc.nextInt();
                }
            }

            int res = 0;

            for (int i = 0; i < n; i++) {
                int startX = trees[i][0];
                int startY = trees[i][1];
                if (startX >= L - (S - 1) || startY>= L - (S - 1)) {
                    continue;
                }
                for (int j = 0; j <= S; j++) {
                    for (int k = 0; k <= S; k++) {
                        temp[j][k] = 0;
                    }
                }
                for (int j = 0; j < n; j++) {
                    int tempx = trees[j][0] - startX;
                    int tempy = trees[j][1] - startY;
                    if (tempx <= S && tempx >= 0 && tempy <= S && tempy >= 0) {
                        temp[tempx][tempy] = 1;
                    }
                }
                boolean flag = true;
                for (int j = 0; j <= S; j++) {
                    for (int k = 0; k <= S; k++) {
                        if (trasurea[j][k] != temp[j][k]) {
                            flag = false;
                            break;
                        }
                    }
                }
                if (flag) {
                    res++;
                }

            }
            System.out.println(res);
        }


    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值