华为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)
相关推荐
yzx99101315 小时前
基于Django的智慧园区管理系统开发全解析
后端·python·django
sunsunyu0316 小时前
视频转图片工具
python·音视频
软件开发技术深度爱好者16 小时前
Python类中方法种类介绍
开发语言·python
用户83562907805116 小时前
使用Python合并Word文档:实现高效自动化办公
后端·python
闭着眼睛学算法17 小时前
【双机位A卷】华为OD笔试之【排序】双机位A-银行插队【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·javascript·c++·python·算法·华为od
Pocker_Spades_A17 小时前
Python快速入门专业版(五十四):爬虫基石:HTTP协议全解析(从请求到响应,附Socket模拟请求)
爬虫·python·http
DoubleKK17 小时前
Python 中的 json_repair 使用教程:轻松修复大模型返回的非法 JSON
python
萧鼎18 小时前
深入掌握 OpenCV-Python:从图像处理到智能视觉
图像处理·python·opencv
海琴烟Sunshine18 小时前
leetcode 190. 颠倒二进制位 python
python·算法·leetcode
淡忘_cx18 小时前
Dify Plugin 开发教程
python