围棋的气-2023年OD统一考试(C卷)

题目描述
围棋棋盘由纵横各19条线垂直相交组成,棋盘上一共19x19=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。

JAVA解法:

public class TestPro {
    private static final int SIZE = 19;
    private static int[][] board = new int[SIZE][SIZE]; // 0: empty, 1: black, 2: white

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] blackInput = scanner.nextLine().split(" ");
        String[] whiteInput = scanner.nextLine().split(" ");

        // Place black stones
        for (int i = 0; i < blackInput.length; i += 2) {
            int row = Integer.parseInt(blackInput[i]);
            int col = Integer.parseInt(blackInput[i + 1]);
            board[row][col] = 1;
        }

        // Place white stones
        for (int i = 0; i < whiteInput.length; i += 2) {
            int row = Integer.parseInt(whiteInput[i]);
            int col = Integer.parseInt(whiteInput[i + 1]);
            board[row][col] = 2;
        }

        // Calculate liberties (breaths)
        int blackLiberties = calculateLiberties(1);
        int whiteLiberties = calculateLiberties(2);

        System.out.println("Black Liberties: " + blackLiberties);
        System.out.println("White Liberties: " + whiteLiberties);
    }

    private static int calculateLiberties(int stoneColor) {
        boolean[][] countedLiberties = new boolean[SIZE][SIZE];
        int liberties = 0;

        for (int row = 0; row < SIZE; row++) {
            for (int col = 0; col < SIZE; col++) {
                if (board[row][col] == stoneColor) {
                    // Check all four directions
                    liberties += checkLiberty(row - 1, col, countedLiberties);
                    liberties += checkLiberty(row + 1, col, countedLiberties);
                    liberties += checkLiberty(row, col - 1, countedLiberties);
                    liberties += checkLiberty(row, col + 1, countedLiberties);
                }
            }
        }

        return liberties;
    }

    private static int checkLiberty(int row, int col, boolean[][] countedLiberties) {
        if (row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] == 0) {
            if (!countedLiberties[row][col]) {
                countedLiberties[row][col] = true;
                return 1;
            }
        }
        return 0;
    }
}

代码思路总结:
这段代码首先读取黑棋和白棋的坐标位置,然后在棋盘上标记这些位置。接着,它遍历棋盘上的每个棋子,计算每个棋子的“气”。为了避免重复计算同色棋子共享的“气点”,使用了一个boolean二维数组来避免了对同一自由点的重复计数。最后,输出黑棋和白棋的“气”的总数。

代码步骤说明:

  1. 初始化一个19x19的棋盘,用来表示棋盘上的每个点的状态(空、黑棋、白棋)。
  2. 根据输入,将黑棋和白棋的位置在棋盘上标记出来。
  3. 遍历棋盘上的每个棋子,计算其“气”。
  4. 对于每个棋子,检查其上下左右四个方向的交点,如果是空的,则增加一口“气”。
  5. 对于同色棋子重合的“气点”,确保只计算一次。
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值