题目

围棋的气

题目描述

Clip_2024-05-16_10-38-17.png

题目分析

题目的要求是计算给出的围棋中黑白棋子的坐标中黑子和白子的气。 有一个条件:同色两个棋子所共有的那个气只能算作一个气。

解题思路

硬算,使用if条件判断语句,判断这个棋子所处的位置,这个位置有三种可能:

  • 棋子在角上:最多有两口气
  • 棋子在边上:最多有三口气
  • 棋子在棋盘里面:最多有四口气

这三种情况都要判断。 此外,因为同色棋子气相交的话,只能算作一个气,所以还要分别设置一个黑白棋子所访问过的棋子位置和气的位置的列表,这样就会防止出现结果错误的情况。

代码实现(Python)

"""
围棋棋盘由纵横各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。

"""


def get_qi(x: int, y: int, all_visited_points: list, black_list: list,
           white_list: list) -> int:
    """
    计算某个点的气
    Args:
        x (int):
        y (int):
        all_visited_points ():
        black_list ():
        white_list ():

    Returns:
        qi_number (int):

    """
    qi_number = 0
    x_plus = x + 1
    x_minus = x - 1
    y_plus = y + 1
    y_minus = y - 1
    if x_minus < 0:
        if y_minus < 0:
            if [x_plus, y] not in all_visited_points and [x_plus, y] not in black_list and [x_plus, y] not in white_list:
                all_visited_points.append([x_plus, y])
                qi_number += 1
            if [x, y_plus] not in all_visited_points and [x, y_plus] not in black_list and [x, y_plus] not in white_list:
                all_visited_points.append([x, y_plus])
                qi_number += 1
        elif y_plus > 18:
            if [x_plus, y] not in all_visited_points and [x_plus, y] not in black_list and [x_plus, y] not in white_list:
                all_visited_points.append([x_plus, y])
                qi_number += 1
            if [x, y_minus] not in all_visited_points and [x, y_minus] not in black_list and [x, y_minus] not in white_list:
                all_visited_points.append([x, y_minus])
                qi_number += 1
        else:
            if [x, y_plus] not in all_visited_points and [x, y_plus] not in black_list and [x, y_plus] not in white_list:
                all_visited_points.append([x, y_plus])
                qi_number += 1
            if [x_plus, y] not in all_visited_points and [x_plus, y] not in black_list and [x_plus, y] not in white_list:
                all_visited_points.append([x_plus, y])
                qi_number += 1
            if [x, y_minus] not in all_visited_points and [x, y_minus] not in black_list and [x, y_minus] not in white_list:
                all_visited_points.append([x, y_minus])
                qi_number += 1
    elif x_plus > 18:
        if y_minus < 0:
            if [x, y_plus] not in all_visited_points and [x, y_plus] not in black_list and [x, y_plus] not in white_list:
                all_visited_points.append([x, y_plus])
                qi_number += 1
            if [x_minus, y] not in all_visited_points and [x_minus, y] not in black_list and [x_minus, y] not in white_list:
                all_visited_points.append([x_minus, y])
                qi_number += 1
        elif y_plus > 18:
            if [x_minus, y] not in all_visited_points and [x_minus, y] not in black_list and [x_minus, y] not in white_list:
                all_visited_points.append([x_minus, y])
                qi_number += 1
            if [x, y_minus] not in all_visited_points and [x, y_minus] not in black_list and [x, y_minus] not in white_list:
                all_visited_points.append([x, y_minus])
                qi_number += 1
        else:
            if [x, y_plus] not in all_visited_points and [x, y_plus] not in black_list and [x, y_plus] not in white_list:
                all_visited_points.append([x, y_plus])
                qi_number += 1
            if [x_minus, y] not in all_visited_points and [x_minus, y] not in black_list and [x_minus, y] not in white_list:
                all_visited_points.append([x_minus, y])
                qi_number += 1
            if [x, y_minus] not in all_visited_points and [x, y_minus] not in black_list and [x, y_minus] not in white_list:
                all_visited_points.append([x, y_minus])
                qi_number += 1
    else:  # x >= 1 and x <= 17
        if y_minus < 0:
            if [x, y_plus] not in all_visited_points and [x, y_plus] not in black_list and [x, y_plus] not in white_list:
                all_visited_points.append([x, y_plus])
                qi_number += 1
            if [x, y_minus] not in all_visited_points and [x, y_minus] not in black_list and [x, y_minus] not in white_list:
                all_visited_points.append([x, y_minus])
                qi_number += 1
            if [x_plus, y] not in all_visited_points and [x_plus, y] not in black_list and [x_plus, y] not in white_list:
                all_visited_points.append([x_plus, y])
                qi_number += 1
        elif y_plus > 18:
            if [x_minus, y] not in all_visited_points and [x_minus, y] not in black_list and [x_minus, y] not in white_list:
                all_visited_points.append([x_minus, y])
                qi_number += 1
            if [x_plus, y] not in all_visited_points and [x_plus, y] not in black_list and [x_plus, y] not in white_list:
                all_visited_points.append([x_plus, y])
                qi_number += 1
            if [x, y_minus] not in all_visited_points and [x, y_minus] not in black_list and [x, y_minus] not in white_list:
                all_visited_points.append([x, y_minus])
                qi_number += 1
        else:
            if [x, y_plus] not in all_visited_points and [x, y_plus] not in black_list and [x, y_plus] not in white_list:
                all_visited_points.append([x, y_plus])
                qi_number += 1
            if [x, y_minus] not in all_visited_points and [x, y_minus] not in black_list and [x, y_minus] not in white_list:
                all_visited_points.append([x, y_minus])
                qi_number += 1
            if [x_minus, y] not in all_visited_points and [x_minus, y] not in black_list and [x_minus, y] not in white_list:
                all_visited_points.append([x_minus, y])
                qi_number += 1
            if [x_plus, y] not in all_visited_points and [x_plus, y] not in black_list and [x_plus, y] not in white_list:
                all_visited_points.append([x_plus, y])
                qi_number += 1

    return qi_number


black_location_string_list = input().strip().split(" ")
white_location_string_list = input().strip().split(" ")

dict_black_white = dict()

dict_black_white["black"] = []
dict_black_white["white"] = []

for i in range(0, len(black_location_string_list), 2):
    the_list = [int(black_location_string_list[i]),
                int(black_location_string_list[i + 1])]
    dict_black_white["black"].append(the_list)

for i in range(0, len(white_location_string_list), 2):
    the_list = [int(white_location_string_list[i]),
                int(white_location_string_list[i + 1])]
    dict_black_white["white"].append(the_list)

# print(dict_black_white)
# {'black': [[0, 5], [8, 9], [9, 10]], 'white': [[5, 0], [9, 9], [9, 8]]}

all_visited_points_white = []
all_visited_points_black = []

qi_black = 0
for black_item in dict_black_white["black"]:
    qi_black += get_qi(black_item[0], black_item[1],
                       all_visited_points_black,
                       dict_black_white["black"],
                       dict_black_white["white"])

qi_white = 0
for white_item in dict_black_white["white"]:
    qi_white += get_qi(white_item[0],
                       white_item[1],
                       all_visited_points_white,
                       dict_black_white["black"],
                       dict_black_white["white"])

print("黑棋气数: ", qi_black, "白棋气数: ", qi_white)

补充

在CSDN上有人也给出了一种更简洁的方法,我的硬算的笨办法也是参考这位博主的文章

https://blog.csdn.net/weixin_41603028/article/details/136208815?