지나가던 개발자

[Python] NYPC 2022 Round 1 인류의 적 모기 퇴치 문제 풀이 본문

PS/Python

[Python] NYPC 2022 Round 1 인류의 적 모기 퇴치 문제 풀이

KwonYongHyeon 2023. 1. 4. 18:27

 

def listHandler(a, b, l):
    try:
        if a < 0 or b < 0:
            return 0
        return l[a][b]
    except:
        return 0

n, m = map(int, input().split(" "))
village = []
for i in range(n):
    village.append(list(map(int, input().split(" "))))
if m == 0:
    print(max(sum(village, [])))
else:
    maximum = sum(sorted(sum(village, []), reverse=True)[0:m*4+1])
    largest = 0
    a = 0
    printed = False
    for i in village:
        b = 0
        for j in i:
            cross = 0
            diagonal = 0
            for i in range(1, m+1):
                cross += listHandler(a+i, b, village)
                cross += listHandler(a-i, b, village)
                cross += listHandler(a, b+i, village)
                cross += listHandler(a, b-i, village)
                diagonal += listHandler(a+i, b+i, village)
                diagonal += listHandler(a+i, b-i, village)
                diagonal += listHandler(a-i, b+i, village)
                diagonal += listHandler(a-i, b-i, village)
            cross += listHandler(a, b, village)
            diagonal += listHandler(a, b, village)
            b += 1
            if max(cross, diagonal) == maximum:
                print(max(cross, diagonal))
                printed = True
                break
            elif max(cross, diagonal) > largest:
                largest = max(cross, diagonal)
        a += 1
    if not printed:
        print(largest)

 
작년 NYPC 참가했을 때 풀고 나서 "아 문제 올라오면 올려야지~"하고 임시저장 해놨는데 이제야 문제가 올라왔다.

Comments