华为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)
相关推荐
万粉变现经纪人5 分钟前
如何解决 pip install cx_Oracle 报错 未找到 Oracle Instant Client 问题
数据库·python·mysql·oracle·pycharm·bug·pip
sw1213895 分钟前
使用Plotly创建交互式图表
jvm·数据库·python
2301_810160958 分钟前
如何为开源Python项目做贡献?
jvm·数据库·python
若惜9 分钟前
selenium自动化测试web自动化测试 框架封装Pom
前端·python·selenium
weixin_4577600015 分钟前
基于pytorch实现LPR模型车牌识别
人工智能·pytorch·python·深度学习·lpr
Zaly.17 分钟前
【Python刷题】LeetCode 3567 子矩阵的最小绝对差
python·leetcode·矩阵
2501_9454235423 分钟前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
吃杠碰小鸡26 分钟前
Python+Ai学习流程
人工智能·python·学习
飞Link27 分钟前
具身智能音频处理核心框架 PyAudio 深度拆解与实战
开发语言·python·音视频
嫂子的姐夫29 分钟前
043-spiderbuf第C3题
爬虫·python·js逆向·逆向