题目
示例:
输入:
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