华为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)
相关推荐
清水白石0081 小时前
突破并行瓶颈:Python 多进程开销全解析与 IPC 优化实战
开发语言·网络·python
Lupino2 小时前
IoT 平台可编程化:基于 Pydantic Monty 构建工业级智能自动化链路
python
清水白石0083 小时前
突破性能瓶颈:深度解析 Numba 如何让 Python 飙到 C 语言的速度
开发语言·python
yunhuibin5 小时前
AlexNet网络学习
人工智能·python·深度学习·神经网络
喵手6 小时前
Python爬虫实战:增量爬虫实战 - 利用 HTTP 缓存机制实现“极致减负”(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·增量爬虫·http缓存机制·极致减负
一个处女座的程序猿O(∩_∩)O6 小时前
Python异常处理完全指南:KeyError、TypeError、ValueError深度解析
开发语言·python
was1726 小时前
使用 Python 脚本一键上传图片到兰空图床并自动复制链接
python·api上传·自建图床·一键脚本
好学且牛逼的马6 小时前
从“Oak”到“虚拟线程”:JDK 1.0到25演进全记录与核心知识点详解a
java·开发语言·python
shangjian0076 小时前
Python基础-环境安装-Anaconda配置虚拟环境
开发语言·python
codeJinger6 小时前
【Python】函数
开发语言·python