华为OD机试 Java 【围棋的气】

题目

题目描述
围棋棋盘由纵横各19条线垂直相交组成,棋盘上一共19 x 19 = 361 个交点,对弈双方一方执白棋,一方执黑棋,落子时只能将棋子置于交点上。

“气”是围棋中很重要的一个概念,某个棋子有几口气,是指其上下左右方向四个相邻的交叉点中,有几个交叉点没有棋子,由此可知:

1、在棋盘的边缘上的棋子最多有 3 口气(黑1),在棋盘角点的棋子最多有2口气(黑2),其他情况最多有4口气(白1)
2、所有同色棋子的气之和叫做该色棋子的气,需要注意的是,同色棋子重合的气点,对于该颜色棋子来说,只能计算一次气,比如下图中,黑棋一共4口气,而不是5口气,因为黑1和黑2中间红色三角标出来的气是两个黑棋共有的,对于黑棋整体来说只能算一个气。
3、本题目只计算气,对于眼也按气计算,如果您不清楚“眼”的概念,可忽略,按照前面描述的规则计算即可。

现在,请根据输入的黑棋和白棋得到坐标位置,计算黑棋和白棋一共各有多少气?

输入描述
输入包含两行数据,如:

0 5 8 9 9 10
5 0 9 9 9 8

1、每行数据以空格分隔,数据个数是2的整数倍,每两个数是一组,代表棋子在棋盘上的坐标;
2、坐标的原点在棋盘左上角点,第一个值是行号,范围从0到18;第二个值是列号,范围从0到18。
3、举例说明:第一行数据表示三个坐标(0, 5)、(8, 9)、(9, 10)
4、第一行表示黑棋的坐标,第二行表示白棋的坐标。
5、题目保证输入两行数据,无空行且每行按前文要求是偶数个,每个坐标不会超出棋盘范围。

输出描述

8 7

两个数字以空格分隔,第一个数代表黑棋的气数,第二个数代表白棋的气数。

解题思路

1、创建棋盘和记录棋子位置:首先创建一个 19x19 的二维数组来表示围棋的棋盘,其中每个元素表示一个交叉点的状态,初始状态为 0 表示没有棋子。然后记录黑棋和白棋的位置坐标。
2、放置棋子:根据记录的黑棋和白棋的坐标,将它们放置到棋盘上。1 表示黑棋,用 2 表示白棋。
3、计算气数:对于每个交叉点需要计算它的气数。如果一个交叉点为空,那么它的气数取决于周围相邻的棋子。我们可以遍历棋盘数组,对于每个空交叉点,检查它周围相邻的四个交叉点是否被黑棋或白棋占据,如果有一个被占据,则该空交叉点是一个气。我们计算黑棋和白棋的气数,并将其累加起来。

代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] blackCoordinates = readCoordinates(scanner);
        int[] whiteCoordinates = readCoordinates(scanner);
        GoGame game = new GoGame(blackCoordinates, whiteCoordinates);
        int[] liberties = game.calculateLiberties();
        System.out.println(liberties[0] + " " + liberties[1]);
    }

    private static int[] readCoordinates(Scanner scanner) {
        String[] coordinates = scanner.nextLine().split(" ");
        int[] result = new int[coordinates.length];
        for (int i = 0; i < coordinates.length; i++) {
            result[i] = Integer.parseInt(coordinates[i]);
        }
        return result;
    }
}

class GoGame {
    private static final int BLACK = 1;
    private static final int WHITE = 2;
    private int[][] board;
    private int[] blackCoordinates;
    private int[] whiteCoordinates;

    public GoGame(int[] blackCoordinates, int[] whiteCoordinates) {
        this.board = new int[19][19];
        this.blackCoordinates = blackCoordinates;
        this.whiteCoordinates = whiteCoordinates;
    }

    private int countLiberties(int color) {
        int libertiesCount = 0;
        int[][] offsets = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };

        for (int i = 0; i < 19; i++) {
            for (int j = 0; j < 19; j++) {
                if (board[i][j] == 0) {
                    boolean isLiberty = false;
                    for (int[] offset : offsets) {
                        int newI = i + offset[0];
                        int newJ = j + offset[1];
                        if (newI >= 0 && newI < 19 && newJ >= 0 && newJ < 19) {
                            if (board[newI][newJ] == color) {
                                isLiberty = true;
                                break;
                            }
                        }
                    }
                    if (isLiberty) {
                        libertiesCount++;
                    }
                }
            }
        }
        return libertiesCount;
    }

    private void placeStones() {
        placeColorStones(blackCoordinates, BLACK);
        placeColorStones(whiteCoordinates, WHITE);
    }

    private void placeColorStones(int[] coordinates, int color) {
        for (int i = 0; i < coordinates.length; i += 2) {
            int x = coordinates[i];
            int y = coordinates[i + 1];
            board[x][y] = color;
        }
    }

    public int[] calculateLiberties() {
        placeStones();
        int blackLiberties = countLiberties(BLACK);
        int whiteLiberties = countLiberties(WHITE);
        return new int[] { blackLiberties, whiteLiberties };
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

codereasy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值