-
무인도 여행코딩 테스트/Level 2 2023. 1. 28. 13:22반응형
https://school.programmers.co.kr/learn/courses/30/lessons/154540
def solution(maps): def solve(start_y, start_x): que = [(start_y, start_x)] food = 0 while que: pos_y, pos_x = que.pop() if (pos_y, pos_x) not in visited: visited.add((pos_y, pos_x)) food += int(maps[pos_y][pos_x]) for diff_x, diff_y in ((1, 0), (-1, 0), (0, 1), (0, -1)): new_x, new_y = pos_x + diff_x, pos_y + diff_y if 0 <= new_y < len(maps) and 0 <= new_x < len(maps[0]) and maps[new_y][new_x] != 'X': que.append((new_y, new_x)) return food visited = set() answer = [] for each_y in range(len(maps)): for each_x in range(len(maps[0])): if (each_y, each_x) not in visited and maps[each_y][each_x] != 'X': answer.append(solve(each_y, each_x)) return sorted(answer) if answer else [-1]
전형적인 완전 탐색 문제...
반응형