๐Ÿ—๏ธ Algorithm/๐ŸŸฉ ๋ฐฑ์ค€

๐ŸŸฉ [๋ฐฑ์ค€] [Python] [Gold4] 15683๋ฒˆ_๊ฐ์‹œ

Dbswnstjd 2023. 4. 5. 20:27

๋ฌธ์ œ

https://www.acmicpc.net/problem/15683

 

15683๋ฒˆ: ๊ฐ์‹œ

์Šคํƒ€ํŠธ๋งํฌ์˜ ์‚ฌ๋ฌด์‹ค์€ 1×1ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋Š” N×M ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ๋ฌด์‹ค์—๋Š” ์ด K๊ฐœ์˜ CCTV๊ฐ€ ์„ค์น˜๋˜์–ด์ ธ ์žˆ๋Š”๋ฐ, CCTV๋Š” 5๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค. ๊ฐ CCTV๊ฐ€ ๊ฐ

www.acmicpc.net

ํ’€์ด

# ๋ฐฑ์ค€ 15683๋ฒˆ ๋ฌธ์ œ - ๊ฐ์‹œ
import sys; input = sys.stdin.readline
import copy

# CCTV ์ข…๋ฅ˜๋ณ„, ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ๋ณ„ ๊ฐ์‹œ์˜์—ญ ์žฌ๊ท€์  ํƒ์ƒ‰
def dfs(graph, depth):
    global answer
    # ์ข…๋ฃŒ ์กฐ๊ฑด: ๋ชจ๋“  CCTV ํƒ์ƒ‰
    if depth == len(cctv_list):
        # ์‚ฌ๊ฐ์ง€๋Œ€ ์ตœ์†Ÿ๊ฐ’
        answer = min(answer, count_zero(graph))
        return
    else:
        # ์‚ฌ๋ฌด์‹ค ์ •๋ณด ๊นŠ์€ ๋ณต์‚ฌ
        graph_copy = copy.deepcopy(graph)
        x, y, cctv_type = cctv_list[depth]
        for cctv_dir in cctv_direction[cctv_type]:
            # CCTV ๊ฐ์‹œ์˜์—ญ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ
            watch(x, y, cctv_dir, graph_copy)
            # ํ˜„์žฌ Case์—์„œ ํƒ€ ๋ชจ๋“  CCTV ์žฌ๊ท€์  ํƒ์ƒ‰
            dfs(graph_copy, depth + 1)
            # CCTV๋ฅผ ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „์‹œํ‚จ ํ›„ ์žฌํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•จ
            graph_copy = copy.deepcopy(graph)

# CCTV ๊ฐ์‹œ์˜์—ญ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
def watch(x, y, direction, graph):
    for d in direction:
        nx, ny = x, y
        # ํŠน์ • ๋ฐฉํ–ฅ์œผ๋กœ ๋ฒฝ์„ ๋งŒ๋‚˜๊ฑฐ๋‚˜ ์‚ฌ๋ฌด์‹ค์„ ๋ฒ—์–ด๋‚˜๊ธฐ ์ „๊นŒ์ง€ ํƒ์ƒ‰
        while True:
            nx += direction_list[d][0]
            ny += direction_list[d][1]
            # ๋งต ๋‚ด ์œ„์น˜
            if 0 <= nx < N and 0 <= ny < M:
                # ๋ฒฝ์„ ๋งŒ๋‚œ ๊ฒฝ์šฐ
                if graph[nx][ny] == 6:
                    break
                # ์ƒˆ๋กœ์šด ๊ฐ์‹œ๊ฐ€๋Šฅ ์˜์—ญ์ผ ๊ฒฝ์šฐ
                elif graph[nx][ny] == 0:
                    graph[nx][ny] = '#'
            # ๋งต ์™ธ ์œ„์น˜
            else:
                break

# ์‚ฌ๊ฐ์ง€๋Œ€ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
def count_zero(graph):
    cnt = 0
    for i in range(N):
        for j in range(M):
            if graph[i][j] == 0:
                cnt += 1
    return cnt

if __name__ == '__main__':
    N, M = map(int, input().split())
    graph = [list(map(int, input().split())) for _ in range(N)]
    # ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์ดˆ๊ธฐ๊ฐ’ 10์–ต ์„ธํŒ…
    answer = int(1e9)
    cctv_list = []
    for i in range(N):
        for j in range(M):
            if 1 <= graph[i][j] <= 5:
                # CCTV ์ขŒํ‘œ ๋ฐ ์ข…๋ฅ˜ ์ €์žฅ
                cctv_list.append((i, j, graph[i][j]))
    # ํƒ์ƒ‰ ๋ฐฉํ–ฅ: ์ƒ, ํ•˜, ์ขŒ, ์šฐ
    direction_list = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    # CCTV๋ณ„ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๋ฐฉํ–ฅ
    cctv_direction = [
        [],
        [[0], [1], [2], [3]], # 1๋ฒˆ CCTV
        [[0, 1], [2, 3]], # 2๋ฒˆ CCTV
        [[0, 2], [0, 3], [1, 2], [1, 3]], # 3๋ฒˆ CCTV
        [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]], # 4๋ฒˆ CCTV
        [[0, 1, 2, 3]] # 5๋ฒˆ CCTV
    ]
    dfs(graph, 0)
    print(answer)

๋ฌธ์ œ๋ฅผ ํ’€๋‹ค๊ฐ€ ๋„์ €ํžˆ ํ•ด๊ฒฐ์„ ํ•˜์ง€ ๋ชปํ•ด์„œ ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ์—์„œ ๊ฐ€์ ธ์™”๋‹ค. 

ํ’€์ด๋Š” ์กฐ๊ธˆ ๋” ์ดํ•ดํ•˜๊ณ  ๋‹ค์‹œ ์“ฐ๋„๋ก ํ•ด์•ผ๊ฒ ๋‹ค. 

 

์ถœ์ฒ˜ : https://heytech.tistory.com/373