华为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)
相关推荐
jiuri_12154 小时前
Docker使用详解:在ARM64嵌入式环境部署Python应用
python·docker·容器
chenchihwen4 小时前
AI代码开发宝库系列:Function Call
人工智能·python·1024程序员节·dashscope
汤姆yu6 小时前
基于python的化妆品销售分析系统
开发语言·python·化妆品销售分析
上去我就QWER6 小时前
Python下常用开源库
python·1024程序员节
程序员杰哥7 小时前
Pytest之收集用例规则与运行指定用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
Jyywww1218 小时前
Python基于实战练习的知识点回顾
开发语言·python
朝朝辞暮i8 小时前
从0开始学python(day2)
python
程序员黄同学9 小时前
Python中的列表推导式、字典推导式和集合推导式的性能和应用场景?
开发语言·python
AI小云9 小时前
【Python高级编程】类和实例化
开发语言·人工智能·python