OJ-0812

题目

在这里插入图片描述
示例:

输入:
0 5 8 9 9 10
5 0 9 9 9 8
输出:
8 7

题解

public class 围棋的气 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] black = in.nextLine().split(" ");
        String[] white = in.nextLine().split(" ");
        String[] black_xy = getXY(black);
        String[] white_xy = getXY(white);

        System.out.println(getCount(black_xy, white_xy) + " " + getCount(white_xy, black_xy));
    }

    private static int getCount(String[] xy_1, String[] xy_2) {
        HashSet<String> hash = new HashSet<>();
        for (String xy : xy_1) {
            hash.add(xy);
            String[] s = xy.split("_");
            int x = Integer.parseInt(s[0]);
            int y = Integer.parseInt(s[1]);
            if (x > 0) {
                hash.add((x - 1) + "_" + y);
            }
            if (x < 18) {
                hash.add((x + 1) + "_" + y);
            }
            if (y > 0) {
                hash.add(x + "_" + (y - 1));
            }
            if (y < 18) {
                hash.add(x + "_" + (y + 1));
            }
        }
        int count = hash.size() - xy_1.length;
        for (String xy : xy_2) {
            if (hash.contains(xy)) {
                count--;
            }
        }
        return count;
    }

    private static String[] getXY(String[] str) {
        String[] xy = new String[str.length / 2];
        for (int i = 0; i < str.length; i += 2) {
            xy[i / 2] = str[i] + "_" + str[i + 1];
        }
        return xy;
    }
}

参考

import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
 
public class Main {
 
    static int maxSide = 18;
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] locBlacks = in.nextLine().split(" ");
        String[] locWhites = in.nextLine().split(" ");
        String[] blacks = transform(locBlacks);
        String[] whites = transform(locWhites);
        System.out.println(counting(blacks, whites) + " " + counting(whites, blacks));
    }
 
 
    static int counting(String[] alias, String[] ememy) {
        Set<String> count = new HashSet<>();
        for (String a : alias) {
            count.add(a);
            String[] loc = a.split("_");
            int x = Integer.parseInt(loc[0]);
            int y = Integer.parseInt(loc[1]);
            if (x > 0) {
                count.add(Integer.toString(x - 1) + "_" + loc[1]);
            }
            if (x < maxSide) {
                count.add(Integer.toString(x + 1) + "_" + loc[1]);
            }
            if (y > 0) {
                count.add(loc[0] + "_" + Integer.toString(y - 1));
            }
            if (y < maxSide) {
                count.add(loc[0] + "_" + Integer.toString(y + 1));
            }
        }
        int res = count.size() - alias.length;
        for (String e : ememy) {
            if (count.contains(e)) {
                res--;
            }
        }
        return res;
    }
 
    static String[] transform(String[] locs) {
        String[] chess = new String[locs.length / 2];
        for (int i = 0; i < locs.length;) {
            chess[i / 2] = locs[i] + "_" + locs[i + 1];
            i += 2;
        }
        return chess;
    }
}

https://blog.csdn.net/weixin_52908342/article/details/135036370

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值