ํฐ์คํ ๋ฆฌ ๋ทฐ
728x90
๋ฐ์ํ
์ฐ๊ตฌ์ ์๋ฆฌ์ฆ
๋ฌธ์
๋ฌธ์ ํ์ด
์์ ๋ค๋ฃฌ ์ฐ๊ตฌ์ 2 ๋ฌธ์ ์์ ์กฐ๊ฑด์ด ์กฐ๊ธ ๋ฌ๋ผ์ง ๋ฌธ์ ์ด๋ค. ์ฐ๊ตฌ์ 2์์๋ ๋ฐ์ด๋ฌ์ค๋ฅผ ์ค์นํ๊ณ , ๋ฐ์ด๋ฌ์ค๊ฐ ์ค์น๊ฐ๋ฅํ ์์น์ด์ง๋ง ์ค์น๋์ด์์ง ์์ ๊ฒฝ์ฐ ํด๋น ์์ญ์๋ ๋ฐ์ด๋ฌ์ค๊ฐ ์ ํ ๊ฐ๋ฅํ๋ค. ํ์ง๋ง ์ด ๋ฌธ์ ์์๋ ๋ฐ์ด๋ฌ์ค๊ฐ ์ค์น ๊ฐ๋ฅํ ์์น์ ๋ชจ๋ ์ค์น๋์ด ์๊ณ , `ํ์ฑํ`, `๋นํ์ฑํ` ์ํ๋ก ๋๋๊ฒ ๋๋ค. ๋ฐ๋ผ์ ์ด๋ฏธ ๋ฐ์ด๋ฌ์ค๊ฐ ์ค์น๋ ๊ณณ์ด๋ฏ๋ก ์ ํ๊ฐ ๋ถ๊ฐ๋ฅํ์ฌ `๋ฐ์ด๋ฌ์ค๊ฐ ์๋ ์์น๋ฅผ ์ ์ธ์์ผ์ผ ํ๋ค๋ ์ฐจ์ด์ `์ด ์๋ค. ๋ํ, `๋ฐ์ด๋ฌ์ค๋ฅผ ์ ํํ์ฌ์ผ ํ๋ ์๋ 0์ ๊ฐ์์ ๊ฐ์์ง๋ค๋ ์ฐจ์ด์ `๋ ์๋ค.
์ฝ๋
from sys import stdin
from collections import deque
from itertools import combinations
from math import inf
def visitable(x, y, visited):
return 0 <= x < n and 0 <= y < n and graph[x][y] != 1 and not visited[x][y]
def bfs(infect, start):
q = deque(start)
dirs = ((0, 1), (0, -1), (1, 0), (-1, 0))
visited = [[False] * n for _ in range(n)]
cur_time, cur_infect = 0, 0
# ์ด๊ธฐ ๋ฐ์ด๋ฌ์ค ํ์ฑํ ์ง์ ๋ฐฉ๋ฌธ ํ์.
for x, y, _ in q:
visited[x][y] = True
while q:
x, y, time = q.popleft()
for dx, dy in dirs:
next_x, next_y = x + dx, y + dy
if visitable(next_x, next_y, visited):
visited[next_x][next_y] = True
if graph[next_x][next_y] == 0:
# ์ค์น๋ ๋ฐ์ด๋ฌ์ค๋ ์ ์ธํ๊ธฐ ์ํจ.
cur_time = time + 1
cur_infect += 1
q.append((next_x, next_y, time + 1))
return cur_time if cur_infect == infect else answer
def find_loc():
global zero, virus
loc = []
for i in range(n):
for j in range(n):
if graph[i][j] == 2:
loc.append([i, j, 0])
if graph[i][j] == 0:
zero += 1
return combinations(loc, m)
if __name__ == '__main__':
n, m = map(int, stdin.readline().split())
graph = [list(map(int, stdin.readline().split())) for _ in range(n)]
zero, answer = 0, inf
for candi in find_loc():
answer = min(answer, bfs(zero, candi))
print(answer if answer != inf else -1)
728x90
๋ฐ์ํ
'๐จโ๐ป ์ฝ๋ฉํ ์คํธ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค: 3190 ๋ฑ (0) | 2020.09.21 |
---|---|
๋ฐฑ์ค: 4811 ์์ฝ (0) | 2020.09.19 |
๋ฐฑ์ค: 17141 ์ฐ๊ตฌ์ 2 (0) | 2020.09.17 |
๋ฐฑ์ค: 3184 ์ (0) | 2020.09.15 |
๋ฐฑ์ค: 1600 ๋ง์ด ๋๊ณ ํ ์์ญ์ด (0) | 2020.09.14 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ