개발자 톡

연습문제 톡 함께하는 효도

파이썬 이렇게 풀면 왜 틀리나요?

등록일
2024-06-19 22:21:02
조회수
438
작성자
mrswjung
from collections import deque

n, m = map(int, input().split())
farm = []
for i in range(n):
    farm.append(list(map(int, input().split())))

positions = []
for i in range(m):
    x, y = map(int, input().split())
    positions.append([x - 1, y - 1])

dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

seconds = 3

def max_bfs(positions, farm):
    sec_3 = []
    for i in positions:
        sec_3_i = []
        queue = deque()
        queue.append([i, 0, farm[i[0]][i[1]]])
        while queue:
            x = queue.popleft()
            if x[1] == seconds:
                sec_3_i.append(x[2])
            else:
                cx = x[0][0]
                cy = x[0][1]
                for dir in range(4):
                    nx = cx + dx[dir]
                    ny = cy + dy[dir]
                    if 0 <= nx < n and 0 <= ny < n:
                        queue.append([[nx, ny], x[1] + 1, x[2] + farm[nx][ny]])
                        farm[cx][cy] = 0
        sec_3.append(max(sec_3_i))
    result = 0
    for i in sec_3:
        result += i
    print(result)




max_bfs(positions, farm)


#함께하는_효도

이 카테고리의 톡 더보기