[๋ฐฑ์ค€/python] 14502๋ฒˆ : ์—ฐ๊ตฌ์†Œ

2021. 10. 6. 22:14

๋ฌธ์ œ

์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๋‹ค. ๋‹คํ–‰ํžˆ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์•„์ง ํผ์ง€์ง€ ์•Š์•˜๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์—ฐ๊ตฌ์†Œ์— ๋ฒฝ์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค.

์—ฐ๊ตฌ์†Œ๋Š” ํฌ๊ธฐ๊ฐ€ N×M์ธ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ง์‚ฌ๊ฐํ˜•์€ 1×1 ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ์—ฐ๊ตฌ์†Œ๋Š” ๋นˆ ์นธ, ๋ฒฝ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๋ฒฝ์€ ์นธ ํ•˜๋‚˜๋ฅผ ๊ฐ€๋“ ์ฐจ์ง€ํ•œ๋‹ค. 

์ผ๋ถ€ ์นธ์€ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜๋ฉฐ, ์ด ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์ƒํ•˜์ขŒ์šฐ๋กœ ์ธ์ ‘ํ•œ ๋นˆ ์นธ์œผ๋กœ ๋ชจ๋‘ ํผ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์ƒˆ๋กœ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๋ฒฝ์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ์ด๋ฉฐ, ๊ผญ 3๊ฐœ๋ฅผ ์„ธ์›Œ์•ผ ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฐ๊ตฌ์†Œ๊ฐ€ ์ƒ๊ธด ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ž.

2 0 0 0 1 1 0
0 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

 

์ด๋•Œ, 0์€ ๋นˆ ์นธ, 1์€ ๋ฒฝ, 2๋Š” ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ๊ณณ์ด๋‹ค. ์•„๋ฌด๋Ÿฐ ๋ฒฝ์„ ์„ธ์šฐ์ง€ ์•Š๋Š”๋‹ค๋ฉด, ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ๋ชจ๋“  ๋นˆ ์นธ์œผ๋กœ ํผ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

2ํ–‰ 1์—ด, 1ํ–‰ 2์—ด, 4ํ–‰ 6์—ด์— ๋ฒฝ์„ ์„ธ์šด๋‹ค๋ฉด ์ง€๋„์˜ ๋ชจ์–‘์€ ์•„๋ž˜์™€ ๊ฐ™์•„์ง€๊ฒŒ ๋œ๋‹ค.

2 1 0 0 1 1 0
1 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 1 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

 

๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง„ ๋’ค์˜ ๋ชจ์Šต์€ ์•„๋ž˜์™€ ๊ฐ™์•„์ง„๋‹ค.

2 1 0 0 1 1 2
1 0 1 0 1 2 2
0 1 1 0 1 2 2
0 1 0 0 0 1 2
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

 

๋ฒฝ์„ 3๊ฐœ ์„ธ์šด ๋’ค, ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์งˆ ์ˆ˜ ์—†๋Š” ๊ณณ์„ ์•ˆ์ „ ์˜์—ญ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์œ„์˜ ์ง€๋„์—์„œ ์•ˆ์ „ ์˜์—ญ์˜ ํฌ๊ธฐ๋Š” 27์ด๋‹ค.

์—ฐ๊ตฌ์†Œ์˜ ์ง€๋„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์•ˆ์ „ ์˜์—ญ ํฌ๊ธฐ์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ง€๋„์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (3 ≤ N, M ≤ 8)

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ง€๋„์˜ ๋ชจ์–‘์ด ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ์นธ, 1์€ ๋ฒฝ, 2๋Š” ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์žˆ๋Š” ์œ„์น˜์ด๋‹ค. 2์˜ ๊ฐœ์ˆ˜๋Š” 2๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 10๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

๋นˆ ์นธ์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ ์ด์ƒ์ด๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์•ˆ์ „ ์˜์—ญ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ •๋‹ต

from itertools import combinations
from copy import deepcopy
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(N)]   # 0: ๋นˆ์นธ, 1: ๋ฒฝ, 2: ๋ฐ”์ด๋Ÿฌ์Šค

answer = 0
dir = [(-1,0), (0, -1), (1,0), (0,1)]

empty, virus = set(), set()
for x in range(N):
    for y in range(M):
        if arr[x][y] == 0:
            empty.add((x, y))
        if arr[x][y] == 2:
            virus.add((x,y))

for space in combinations(empty, 3):
    _arr = deepcopy(arr)
    for i, j in space:
        _arr[i][j] = 1

    _virus = deepcopy(virus)
    while _virus:
        x, y = _virus.pop()
        for d in dir:
            _x, _y = x+d[0], y+d[1]
            if 0 <= _x < N and 0 <= _y < M and _arr[_x][_y] == 0:
                _virus.add((_x, _y))
                _arr[_x][_y] = 2    

    cnt = 0
    for x in range(N):
        for y in range(M):
            if _arr[x][y] == 0:
                cnt += 1

    answer = max(answer, cnt)

print(answer)

๋นˆ์นธ์ธ ์ƒํƒœ ์ฆ‰, 0์ธ ์œ„์น˜๋ฅผ empry์— ์ €์žฅํ•˜๊ณ 

๋ฐ”์ด๋Ÿฌ์Šค ์ƒํƒœ ์ฆ‰, 2์ธ ์œ„์น˜๋ฅผ virus์— ์ €์žฅํ•œ๋‹ค.

 

combinations๋ฅผ ์ด์šฉํ•˜์—ฌ 3๊ฐœ์”ฉ ๋ฝ‘์•„ ๋ฒฝ์„ ์„ธ์šฐ๊ณ 

virus ์ƒํƒœ์ธ ์นธ์˜ ์ƒํ•˜์ขŒ์šฐ๋กœ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผํŠธ๋ฆฐ๋‹ค.

ํผ์ง„ ์นธ ๋˜ํ•œ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜์—ฌ ํ•ด๋‹น ์นธ์˜ ์ƒํ•˜์ขŒ์šฐ๋„ ์‚ดํ•„ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

 

virus๋ฅผ ๋ชจ๋‘ ํผํŠธ๋ฆฐ ํ›„ ๋นˆ์นธ 0์ธ ์œ„์น˜๋ฅผ ์„ธ์–ด answer์™€ ๋น„๊ตํ•œ๋‹ค.

 

๐Ÿ’ฅ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณต์‚ฌํ•  ๋•Œ๋Š” deepcopy๋ฅผ ์ด์šฉํ•˜๊ธฐ !

๐Ÿ’ฅ ๊ฐ’์„ ๋ฐ”๊ฟ€ ๋•Œ๋Š” == ์ด ์•„๋‹Œ = !!

728x90

BELATED ARTICLES

more