ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

์—ฐ๊ตฌ์†Œ ์‹œ๋ฆฌ์ฆˆ

 

๋ฌธ์ œ

 

17141๋ฒˆ: ์—ฐ๊ตฌ์†Œ 2

์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์— ์Šน์›์ด๊ฐ€ ์นจ์ž…ํ–ˆ๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์œ ์ถœํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์Šน์›์ด๋Š” ์—ฐ๊ตฌ์†Œ์˜ ํŠน์ • ์œ„์น˜์— ๋ฐ”์ด๋Ÿฌ์Šค M๊ฐœ๋ฅผ ๋†“์„ ๊ฒƒ์ด๊ณ , ์Šน์›์ด์˜ ์‹ ํ˜ธ์™€ ๋™์‹œ์— ๋ฐ”์ด๏ฟฝ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์—ฐ๊ตฌ์†Œ์— ๋ฐ”์ด๋Ÿฌ์Šค ์„ค์น˜ ๊ฐ€๋Šฅํ•œ ์œ„์น˜๊ฐ€ ์žˆ๊ณ , ์„ค์น˜ ๊ฐ€๋Šฅํ•œ ๋ฐ”์ด๋Ÿฌ์Šค ์ˆ˜๊ฐ€ M๊ฐœ๋‹ค. ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ํ•˜๋ฃจ๋งˆ๋‹ค ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋กœ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์ „ํŒŒ์‹œํ‚จ๋‹ค. ์ด๋•Œ ์–ด๋–ค ์œ„์น˜์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค M๊ฐœ๋ฅผ ์„ค์น˜ํ•˜์—ฌ, ์ตœ์†Œ ๋‚ ์งœ๋กœ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๋ชจ๋‘ ์ „ํŒŒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์ง€๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค. ์—ฐ๊ตฌ์‹ค ๋‚ด์— ๋ฒฝ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ตฌ๊ฐ„์— ๋ฐ”์ด๋Ÿฌ์Šค ์ „ํŒŒ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 ์•ž์„œ ๋‹ค๋ฃฌ ์—ฐ๊ตฌ์†Œ์—์„œ๋Š” DFS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง์ ‘ ์กฐํ•ฉ์„ ๊ตฌํ•˜์˜€์ง€๋งŒ, itertools๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. (๋‹จ, ์‚ผ์„ฑ ์—ญ๋Ÿ‰ํ…Œ์ŠคํŠธ์—์„œ๋Š” itertools๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.) ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋กœ์ง์„ ํ†ตํ•ด ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

  • DFS ๋˜๋Š” itertools๋ฅผ ํ™œ์šฉํ•˜์—ฌ, ๋ฐ”์ด๋Ÿฌ์Šค M๊ฐœ๊ฐ€ ์„ค์น˜๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค.
  • ๊ฐ ๊ฒฝ์šฐ์˜ ์ˆ˜๋งˆ๋‹ค, BFS๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ์—ผ์‹œํ‚จ ์ˆ˜๋Š” (0์˜ ๊ฐœ์ˆ˜ + ๋ฐ”์ด๋Ÿฌ์Šค ์ˆ˜ - M)๊ณผ ๊ฐ™์œผ๋ฉด ๋ชจ๋‘ ๊ฐ์—ผ๋œ ๊ฒƒ์ด๋‹ค.
    • ๊ฐ์—ผ์‹œํ‚ค๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ๊ฐ’๋“ค ์ค‘ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ๋Š”๋‹ค.
  • ๋ชจ๋“  ๊ฒฝ์šฐ์—์„œ (0์˜ ๊ฐœ์ˆ˜ + ๋ฐ”์ด๋Ÿฌ์Šค ์ˆ˜ - M)์™€ ๊ฐ™์ง€ ์•Š์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์ฝ”๋“œ

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
                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:
                # ํ์—์„œ time ์ •๋ณด๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด 0์„ ์ถ”๊ฐ€ํ•จ.
                loc.append([i, j, 0])
                virus += 1
            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, virus, answer = 0, 0, inf
    
    for candi in find_loc():
        answer = min(answer, bfs(zero + virus - m, candi))
    print(answer if answer != inf else -1)
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€