不要二

链接:https://www.nowcoder.com/questionTerminal/1183548cd48446b38da501e58d5944eb?toCommentId=5410037
来源:牛客网

二货小易有一个W*H的网格盒子,网格的行编号为0H-1,网格的列编号为0W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。


import java.util.*;
public class Main{
    static class Cake {
        int x;
        int y;
        boolean isFlag;
        public Cake(int x, int y, boolean isFlag) {
            this.x = x;
            this.y = y;
            this.isFlag = isFlag;
        }
        public void setFlag(boolean flag) {
            isFlag = flag;
        }
    }
 
    static int count = 0;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int w = scanner.nextInt();
        int h = scanner.nextInt();
        count = w*h;
        List<List<Cake>> lists = new ArrayList<>();
        for (int i = 0; i < h; i++) {
            List<Cake> list = new ArrayList<>();
            for (int j = 0; j < w; j++) {
                list.add(new Cake(i,j,true));
            }
            list.add(null);
            list.add(null);
            lists.add(list);
        }
        lists.add(null);
        lists.add(null);
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                Cake curCake = lists.get(i).get(j);
                if (!curCake.isFlag ){
                    continue;
                }
                Cake rightCake = lists.get(i).get(j + 2);
                if (rightCake != null && rightCake.isFlag){
                    rightCake.setFlag(false);
                    count--;
                }
                List listLevel = lists.get(i + 2);
                if (listLevel != null ){
                    Cake topCake = lists.get(i + 2).get(j);
                    if (topCake.isFlag){
                        topCake.setFlag(false);
                        count--;
                    }
                }
            }
        }
        System.out.println(count);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值