CSP 2019-12回収局アドレスpython実現

10845 ワード

CSP 2019-12回収局アドレスpython実現
時間複雑度O(n)
#test_csp_     
def solve(alist, numlist):
    dic = {
     }
    for i in alist:     #    x     y,   
        if i[0] in dic:
            dic[i[0]].append(i[1])
        else:
            dic[i[0]] = [i[1]]
    for i in alist:
        count = 0
        try:
            if (i[1]+1 in dic[i[0]]) and (i[1]-1 in dic[i[0]]) and\
               (i[1] in dic[i[0]+1]) and (i[1] in dic[i[0]-1]):     #      
                if i[1]+1 in dic[i[0]-1]:   #   
                    count += 1
                if i[1]+1 in dic[i[0]+1]:   #   
                    count += 1
                if i[1]-1 in dic[i[0]-1]:   #   
                    count += 1
                if i[1]-1 in dic[i[0]+1]:   #   
                    count += 1
                numlist[count] += 1
        except:
            continue

def main():
    n = eval(input())
    alist = []
    numlist = [0 for _ in range(5)]
    for i in range(n):
        x, y = map(int, input().split(' '))
        alist.append([x, y])
    solve(alist, numlist)
    for i in numlist:
        print(i)

if __name__ == "__main__":
    main()

'''
7
1 2
2 1
0 0
1 1
1 0
2 0
0 1
'''
'''
11
9 10
10 10
11 10
12 10
13 10
11 9
11 8
12 9
10 9
10 11
12 11

'''