华为OD机试真题-围棋的气-2023年OD统一考试(C卷)---python代码

题目:


代码:

python 复制代码
"""
# 输入:2的倍数
第一个为行号 0-18
第二个为列号 0-18

第一行为黑色
第二行为白色

思路:先求黑色,进行去重+棋子的位置,再求白色
逐个棋子求坐标。
"""
def get_item(a):
    temp = list()
    for item in a:
        row = item[0]
        column = item[1]
        if row - 1 >= 0:
            temp.append([row - 1, column])
        if column - 1 >= 0:
            temp.append([row, column - 1])
        if row + 1 <= 18:
            temp.append([row + 1, column])
        if column + 1 <= 18:
            temp.append([row, column + 1])
    return temp


# 输入不固定,2的整数倍
b = list(map(int, input().split()))
w = list(map(int, input().split()))
black = [b[i:i+2] for i in range(0, len(b), 2)]
white = [w[i:i+2] for i in range(0, len(w), 2)]
# black = [[0,5], [8,9], [9,10]]
# white = [[5,0], [9,9], [9,8]]

temp_black = get_item(black)
temp_white = get_item(white)
# 将子列表转换为元组并用set去重,然后再转换回列表
black_set_temp = set(tuple(sublist) for sublist in temp_black)
white_set = set(tuple(sublist) for sublist in white)
black_set = set(tuple(sublist) for sublist in black)
# 求当前黑色棋子红周围的气与白色求交集,求当前黑色的周围的气与本身黑色的交集,这两个交集求并集,用当前黑色棋子周围的气求并集的差集
black_res = black_set_temp - ((black_set_temp & white_set) | (black_set_temp & black_set))

white_set_temp = set(tuple(sublist) for sublist in temp_white)

white_res = white_set_temp - ((white_set_temp & black_set) | (white_set_temp & white_set))
# 本身去重
print(len(black_res), black_res)
print(len(white_res), white_res)
相关推荐
烛阴2 小时前
简单入门Python装饰器
前端·python
好开心啊没烦恼2 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
面朝大海,春不暖,花不开2 小时前
使用 Python 实现 ETL 流程:从文本文件提取到数据处理的全面指南
python·etl·原型模式
2301_805054563 小时前
Python训练营打卡Day59(2025.7.3)
开发语言·python
万千思绪4 小时前
【PyCharm 2025.1.2配置debug】
ide·python·pycharm
微风粼粼5 小时前
程序员在线接单
java·jvm·后端·python·eclipse·tomcat·dubbo
云天徽上5 小时前
【PaddleOCR】OCR表格识别数据集介绍,包含PubTabNet、好未来表格识别、WTW中文场景表格等数据,持续更新中......
python·ocr·文字识别·表格识别·paddleocr·pp-ocrv5
你怎么知道我是队长6 小时前
python-input内置函数
开发语言·python
叹一曲当时只道是寻常6 小时前
Python实现优雅的目录结构打印工具
python
hbwhmama7 小时前
python高级变量XIII
python