CSP 201912-2 回收站选址 python实现+详解

试题

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码

# 读入点的个数
n = int(input())

# 创建字典,键为点的坐标,值为true,存放所有点
dict = {
    
    }
for i in range(n):
    x, y = [int(m) for m in input().split()]
    dict[(x, y)] = 'true'

# 创建字典,存放是回收站的点
dict1 = {
    
    }
for key in dict.keys():
    x, y = key[0], key[1]
    if dict.get((x, y-1)) == dict.get((x, y+1)) == dict.get((x-1, y)) == dict.get((x+1, y)) == 'true':
        dict1[(x, y)] = 'true'
 

# 当不存在是回收站的点
if len(dict1) == 0:
    print(0)
    print(0)
    print(0)
    print(0)
    print(0)
else:  # 创建列表,存放得分为0,1,2,3,4的回收站选址个数
    list = [0]*5
    for key in dict1.keys():
        x, y = key[0], key[1]
        score = 0
        if dict.get((x-1, y-1)) == 'true':
            score += 1
        if dict.get((x-1, y+1)) == 'true':
            score += 1
        if dict.get((x+1, y-1)) == 'true':
            score += 1
        if dict.get((x+1, y+1)) == 'true':
            score += 1
        list[score] += 1
    for i in range(5):
        print(list[i])

猜你喜欢

转载自blog.csdn.net/weixin_44997802/article/details/108374786