ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€(BOJ): 15683. ๊ฐ์‹œ
    ์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ 2023. 2. 23. 18:43

    ๐Ÿ’๐Ÿป  ๋ฌธ์ œ

     

    15683๋ฒˆ: ๊ฐ์‹œ

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

    www.acmicpc.net


    ๐Ÿคท๐Ÿผ‍โ™€๏ธ  ๋ฌธ์ œ ์„ค๋ช…

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

    1๋ฒˆ CCTV๋Š” ํ•œ ์ชฝ ๋ฐฉํ–ฅ๋งŒ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. 2๋ฒˆ๊ณผ 3๋ฒˆ์€ ๋‘ ๋ฐฉํ–ฅ์„ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, 2๋ฒˆ์€ ๊ฐ์‹œํ•˜๋Š” ๋ฐฉํ–ฅ์ด ์„œ๋กœ ๋ฐ˜๋Œ€๋ฐฉํ–ฅ์ด์–ด์•ผ ํ•˜๊ณ , 3๋ฒˆ์€ ์ง๊ฐ ๋ฐฉํ–ฅ์ด์–ด์•ผ ํ•œ๋‹ค. 4๋ฒˆ์€ ์„ธ ๋ฐฉํ–ฅ, 5๋ฒˆ์€ ๋„ค ๋ฐฉํ–ฅ์„ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.

    • CCTV๋Š” ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์— ์žˆ๋Š” ์นธ ์ „์ฒด๋ฅผ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ๋ฌด์‹ค์—๋Š” ๋ฒฝ์ด ์žˆ๋Š”๋ฐ, CCTV๋Š” ๋ฒฝ์„ ํ†ต๊ณผํ•  ์ˆ˜ ์—†๋‹ค.
      CCTV๊ฐ€ ๊ฐ์‹œํ•  ์ˆ˜ ์—†๋Š” ์˜์—ญ์€ ์‚ฌ๊ฐ์ง€๋Œ€๋ผ๊ณ  ํ•œ๋‹ค.
    • CCTV๋Š” ํšŒ์ „์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํšŒ์ „์€ ํ•ญ์ƒ 90๋„ ๋ฐฉํ–ฅ์œผ๋กœ ํ•ด์•ผ ํ•˜๋ฉฐ, ๊ฐ์‹œํ•˜๋ ค๊ณ  ํ•˜๋Š” ๋ฐฉํ–ฅ์ด ๊ฐ€๋กœ ๋˜๋Š” ์„ธ๋กœ ๋ฐฉํ–ฅ์ด์–ด์•ผ ํ•œ๋‹ค.

    ์‚ฌ๋ฌด์‹ค์˜ ํฌ๊ธฐ์™€ ์ƒํƒœ, ๊ทธ๋ฆฌ๊ณ  CCTV์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, CCTV์˜ ๋ฐฉํ–ฅ์„ ์ ์ ˆํžˆ ์ •ํ•ด์„œ, ์‚ฌ๊ฐ ์ง€๋Œ€์˜ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

     

    โš™๏ธ  ํ’€์ด

    DFS + ๊ตฌํ˜„ ๋ฌธ์ œ๋‹ค.
    ๊ฐ๊ฐ์˜ CCTV๊ฐ€ ํšŒ์ „ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์™€ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์— ๋Œ€ํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋งŒ ์ž˜ ์ •๋ฆฌํ•ด๋‚ด๋ฉด ๋œ๋‹ค.
    ์ž…๋ ฅ๊ฐ’์˜ ๋ฒ”์œ„๊ฐ€ 1 ≤ N, M ≤ 8๋กœ ๋งค์šฐ ์ž‘๊ธฐ ๋•Œ๋ฌธ์—, ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์–ด๋ณด์ธ๋‹ค.
    ๊ฐ๊ฐ์˜ CCTV์˜ ํšŒ์ „ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋Š” ๋ชจ๋“  ์‚ฌ๊ฐ์ง€๋Œ€๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ , ๊ทธ ์ค‘ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•ด๋‚ด๋ฉด ๋œ๋‹ค.

     

    ๊ตฌํ˜„๋ฌธ์ œ์น˜๊ณ  ์ง€๋ฌธ์ด ์•„์ฃผ ์นœ์ ˆํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์•„์„œ, ๊ฐœ์ธ์ ์œผ๋กœ ๊ณจ๋“œ4 ๋‚œ์ด๋„๊นŒ์ง€๋Š” ์•„๋‹ˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
    ์—ญ์‹œ ๊ตฌํ˜„์€ ์กฐ๊ธˆ ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ ๊ฐ™์•„ ๋ถˆ์•ˆํ•˜๋”๋ผ๋„ ์ฝ”๋“œ๋ฅผ ์งœ๊ธฐ ์ „์— ์„ค๊ณ„๋ฅผ ๊ผผ๊ผผํ•˜๊ฒŒ ํ•ด์•ผ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๋‹ค์‹œ ๋Š๊ผˆ๋‹ค.

    import sys
    from itertools import product
    from copy import deepcopy
    
    # ๊ฐ๊ฐ์˜ cctv ๋ฒˆํ˜ธ๋ฅผ key๋กœ, 
    # ํšŒ์ „์— ๋”ฐ๋ฅธ ๊ฐ์‹œ ๊ฐ€๋Šฅํ•œ ๋ฐฉํ–ฅ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜์˜ list๋ฅผ value๋กœ ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ •์˜
    cctv = {
        1: [['right'], ['left'], ['up'], ['down']],
        2: [['right', 'left'], ['up', 'down']],
        3: [
            ['up', 'right'],
            ['right', 'down'],
            ['down', 'left'],
            ['left', 'up']]
        ,
        4: [['left', 'up', 'right'], ['up', 'right', 'down'], ['right', 'down', 'left'], ['down', 'left', 'up']],
        5: [['left', 'right', 'up', 'down']]
    }
    
    
    def dfs(x, y, key, v_idx):
        global N, M, d_arr
        # cctv ๋ฒˆํ˜ธ, ํšŒ์ „๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์„ ์„ค์ •
        direction = cctv[key][v_idx]
        # ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ์„ ํƒ์ƒ‰
        for d in direction:
            if d == 'up':
                loc = x
                while loc - 1 >= 0:
                    if d_arr[loc - 1][y] == 6:
                        break
                    if d_arr[loc - 1][y] == 0:
                        d_arr[loc - 1][y] = '#'
                    loc -= 1
            elif d == 'right':
                loc = y
                while loc + 1 < M:
                    if d_arr[x][loc + 1] == 6:
                        break
                    if d_arr[x][loc + 1] == 0:
                        d_arr[x][loc + 1] = '#'
                    loc += 1
            elif d == 'left':
                loc = y
                while loc - 1 >= 0:
                    if d_arr[x][loc - 1] == 6:
                        break
                    if d_arr[x][loc - 1] == 0:
                        d_arr[x][loc - 1] = '#'
                    loc -= 1
            elif d == 'down':
                loc = x
                while loc + 1 < N:
                    if d_arr[loc + 1][y] == 6:
                        break
                    if d_arr[loc + 1][y] == 0:
                        d_arr[loc + 1][y] = '#'
                    loc += 1
    
    
    def count_blind_spot():
        global N, M, d_arr
        result = 0
        for x in range(N):
            for y in range(M):
                if d_arr[x][y] == 0:
                    result += 1
        return result
    
    
    N, M = map(int, sys.stdin.readline().split())
    arr = []
    cctv_list = []
    for i in range(N):
        temp = list(map(int, sys.stdin.readline().split()))
        arr.append(temp)
        for j in range(M):
            if 1 <= temp[j] <= 5:
                cctv_list.append((i, j))
                
    # cctv์˜ ์ข…๋ฅ˜, ํšŒ์ „๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•œ ๋ฐฐ์—ด          
    total_cctv_list = list(product(*[[t for t in range(len(cctv[arr[i][j]]))] for i, j in cctv_list]))
    
    answer = float('inf')
    for data in total_cctv_list:
        d_arr = deepcopy(arr)
        # ๊ฐ์‹œ์˜์—ญ์„ ๊ฐฑ์‹ 
        for i, cctv_dir in enumerate(data):
            loc_i, loc_j = cctv_list[i]
            cctv_key = arr[loc_i][loc_j]
            dfs(loc_i, loc_j, cctv_key, cctv_dir)
        d_answer = count_blind_spot()
        # ์‚ฌ๊ฐ์ง€๋Œ€(d_answer)์˜ ์ตœ์†Œ ํฌ๊ธฐ ๊ฐฑ์‹ 
        answer = min(d_answer, answer)
    print(answer)

     

     

    ๋Œ“๊ธ€

Designed by Tistory.