CCF CSP 2019-2 回收站选址 垃圾站选址
一、题目
二、分析
这道题很有意思,如果用C/C++来解题,可能会有很多精彩的解法。
本打算用字典解决,但解题过程中发现Node上储存的数据比较简单,只是 “有” 或 “没有” 垃圾 的 Bool 型,直接将Bool型过滤掉,可以直接用list解决。
三、代码
如同前篇,仓促之作,应该有更好的代码,欢迎交流指正~
n=int(input())
trashDict=[]
listKeys=[]
listScoreCount=[0,0,0,0,0]
def toStr(x,y):
return str(x)+' '+str(y)
def toFind(x,y):
return toStr(x+1,y) in trashDict and\
toStr(x-1,y) in trashDict and\
toStr(x,y+1) in trashDict and\
toStr(x,y-1) in trashDict
def getScore(x,y):
score=0;
if toStr(x+1,y+1) in trashDict:
score+=1
if toStr(x+1,y-1) in trashDict:
score+=1
if toStr(x-1,y+1) in trashDict:
score+=1
if toStr(x-1,y-1) in trashDict:
score+=1
listScoreCount[score]+=1
for i in range(n):
temp_a,temp_b=map(int,input().split(' '))
listKeys.append(temp_a)
listKeys.append(temp_b)
trashDict.append(toStr(temp_a,temp_b))
for j in range(0,2*n,2):
x,y=listKeys[j],listKeys[j+1]
if toFind(x,y):
getScore(x,y)
print(*listScoreCount,sep='\n')