[BOJ] 15683. ๊ฐ์‹œ (๐Ÿฅ‡ , ๊ตฌํ˜„/์‹œ๋ฎฌ๋ ˆ์ด์…˜)

lemythe423ยท2023๋…„ 5์›” 29์ผ
0

BOJ ๋ฌธ์ œํ’€์ด

๋ชฉ๋ก ๋ณด๊ธฐ
38/133
post-thumbnail

๋ฌธ์ œ

๊ฐ cctv์˜ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๊ฐ์‹œ ์ง€์—ญ์„ ์„ค์ •ํ•˜๊ณ , ๊ฐ์‹œ ๋˜์ง€ ์•Š๋Š” ์‚ฌ๊ฐ์ง€๋Œ€์˜ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

ํ’€์ด

๋ฐฑ์ค€ ์—ฐ๊ตฌ์†Œ ์‹œ๋ฆฌ์ฆˆ๋ž‘ ์ข€ ๋น„์Šทํ•˜๋‹ค๊ณ  ๋Š๊ผˆ๋‹ค.

  1. cctv์˜ ๋ฒˆํ˜ธ์™€ ์œ„์น˜๋ฅผ ๊ตฌํ•œ๋‹ค
  2. ๊ฐ cctv๋ฅผ ๋Œ๋ฉด์„œ ํ•˜๋‚˜์”ฉ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๊ฐ์‹œ์ง€์—ญ์„ ์„ค์ •ํ•œ๋‹ค. ์ฒ˜์Œ์—” ๋ถ์ชฝ ๋ฐฉํ–ฅ, ๊ทธ ๋‹ค์Œ์—” ๋‚จ์ชฝ ๋ฐฉํ–ฅ ๋“ฑ๋“ฑ...
  3. 1๋ฒˆ cctv์˜ ๊ฐ์‹œ์ง€์—ญ์„ ์„ค์ •ํ–ˆ๋‹ค๋ฉด ๊ทธ ๋‹ค์Œ์—” ์žฌ๊ท€๋ฅผ ๋Œ๋ ค์„œ 2๋ฒˆ ์นด๋ฉ”๋ผ์˜ cctv๋ฅผ ์„ค์ •ํ•˜๋Š” ๋“ฑ.. ์ด๋Ÿฐ์‹์œผ๋กœํ•ด์„œ ๋งˆ์ง€๋ง‰ cctv์˜ ๊ฐ์‹œ์ง€์—ญ๊นŒ์ง€ ์ฒ˜๋ฆฌํ–ˆ๋‹ค๋ฉด ์ตœ์ข…์ ์œผ๋กœ ๋ช‡ ๊ฐœ์˜ ์ง€์—ญ์ด ์ฒ˜๋ฆฌ๋๋Š”์ง€ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋ฉด ์‚ฌ๊ฐ์ง€๋Œ€์˜ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Œ

์ฝ”๋“œ

๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” v๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์ตœ๋‹จ ์‹œ๊ฐ„ ์ฝ”๋“œ๋“ค์„ ๋ณด๋ฉด์„œ ๋ฆฌํŒฉํ† ๋งํ•ด์•ผ ํ•  ๊ฑฐ ๊ฐ™๋‹ค.

# 348ms

def cctv(c, n, t=1):
    cctvs, i = [], 0
    while i < n:
        tmp, j = [], 0
        while j < c:
            tmp.append(dirs[(i+j*t)%4])
            j += 1
        cctvs.append(tmp)
        i += 1

    return cctvs

# cctv๋“ค์˜ ์œ„์น˜๋ž‘ ๋ฒˆํ˜ธ ์ฐพ์•„์„œ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
def cctv_wall():
    ctv, walls = [], 0
    for i in range(1, N+1):
        for j in range(1, M+1):
            if 0<arr[i][j]<6:
                ctv.append((arr[i][j], i, j))
            elif arr[i][j] == 6:
                walls += 1
    return ctv, walls, len(ctv)+walls

def sol(k, v, cnt):
    global max_cnt
    if k == len(ctvs):
        max_cnt = max(max_cnt, cnt)
        return 
    
    num, i, j = ctvs[k]
    for dir in ctv_num[num]:
        c = 0
        tmp_v = [row[:] for row in v]
        for di, dj in dir:
            si, sj = i+di, j+dj
            while (a:=arr[si][sj])!=6:
                if a == 0 and v[si][sj] != '#':
                    c += 1
                    v[si][sj] = '#'
                si += di
                sj += dj
        sol(k+1, v, cnt+c)
        v = [row[:] for row in tmp_v]


N, M = map(int, input().split())
arr = [[6]*(M+2)] +[[6]+list(map(int, input().split()))+[6] for _ in range(N)]+[[6]*(M+2)]
dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ctv_num = {
    1: cctv(1, 4), 
    2: cctv(2, 2, 2), 
    3: cctv(2, 4),
    4: cctv(3, 4), 
    5: cctv(4, 1)
}

visit = [[0]*(M+2) for _ in range(N+2)]
ctvs, walls, ctvwall = cctv_wall()
max_cnt = 0


sol(0, visit, 0)
print(N*M-ctvwall-max_cnt)

์ฐธ๊ณ ํ•œ ์ฝ”๋“œ

๋ฐฑ์ค€์— ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ  ํ–ˆ๋Š”๋ฐ, ์šฐ์„  cctv์˜ ์œ„์น˜์™€ ๋ฒˆํ˜ธ ์ •๋ณด๋ฅผ ์–ป์„ ๋•Œ, ์ด๋ฏธ ๊ทธ ํ•˜๋‚˜์˜ cctv์—์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ์‹œ ์ง€์—ญ์˜ ์ •๋ณด๋ฅผ ์ „๋ถ€ ๊ตฌํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ทธ ์œ„์น˜ ๋ฐ์ดํ„ฐ๋“ค์„ set()์— ๋„ฃ๊ณ  ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ์—†์ด ์žฌ๊ท€๋ฅผ ๋Œ๋ฆฌ๋ฉด ์ตœ์ข…์ ์œผ๋กœ ๊ตฌํ•ด์ง„ set(๊ฐ์‹œ ์ง€์—ญ ์ •๋ณด)๋“ค์„ ๊ฐ€์ง€๊ณ  ์‚ฌ๊ฐ์ง€๋Œ€์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ.

  • cctv์˜ ์œ„์น˜์™€ ๋ฐฐ์—ด ๊ฐ’๋“ค์€ ๋ณ€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ cctv๋งˆ๋‹ค ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์—ญ๋“ค์„ ์ตœ์ดˆ๋กœ ๋”ฑ ํ•œ ๋ฒˆ ๊ตฌํ•ด๋‘๋ฉด ๋œ๋‹ค.
def watch(i, j, *cctv):
    global watch_spot
    spot_list = []
    for dir in cctv:
        spot = set()
        for d in dir:
            ni, nj = i+di[d], j+dj[d]
            while arr[ni][nj] != 6:
                if not arr[ni][nj]:
                    spot.add((ni, nj))
                ni += di[d]
                nj += dj[d]
        spot_list.append(spot)
    
    watch_spot[(i, j)] = spot_list
  • cctv๋“ค์ด ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์—ญ์˜ ๋ฐฉํ–ฅ๋“ค์— ๋Œ€ํ•ด์„œ๋Š” ์ง์ ‘ ์ž…๋ ฅํ•ด์คฌ๋‹ค. for๋ฌธ์„ ์“ฐ๋Š” ๊ฒƒ๋ณด๋‹ค ๋œ ๋ณต์žก
for i in range(1, N+1):
    for j in range(1, M+1):
        a = arr[i][j]
        if a == 0:
            zero_spot += 1
        elif a == 1:
            watch(i, j, [0], [1], [2], [3])
        elif a == 2:
            watch(i, j, [0, 2], [1, 3])
        elif a == 3:
            watch(i, j, [0, 1], [1, 2], [2, 3], [3, 0])
        elif a == 4:
            watch(i, j, [0, 1, 2], [1, 2, 3], [2, 3, 0], [3, 0, 1])
        elif a == 5:
            watch(i, j, [0, 1, 2, 3])

๋ฆฌํŒฉํ† ๋ง ํ›„ ์ฝ”๋“œ

  • 104ms
import sys
input = sys.stdin.readline

def watch(i, j, *cctv):
    global watch_spot
    spot_list = []
    for dir in cctv:
        spot = set()
        for d in dir:
            ni, nj = i+di[d], j+dj[d]
            while arr[ni][nj] != 6:
                if not arr[ni][nj]:
                    spot.add((ni, nj))
                ni += di[d]
                nj += dj[d]
        spot_list.append(spot)
    
    watch_spot[(i, j)] = spot_list
                
def sol(i, spots, K):
    global blind
    if i == K:
        blind = min(blind, zero_spot-len(spots))
        return 
    
    for watch_list in watch_spot[cctvs[i]]:
        sol(i+1, spots | watch_list, K)

N, M = map(int, input().split())
arr = [[6]*(M+2)] +[[6]+list(map(int, input().split()))+[6] for _ in range(N)]+[[6]*(M+2)]
di, dj = (-1, 0, 1, 0), (0, 1, 0, -1)

zero_spot, blind = 0, 1e13
watch_spot = dict()
for i in range(1, N+1):
    for j in range(1, M+1):
        a = arr[i][j]
        if a == 0:
            zero_spot += 1
        elif a == 1:
            watch(i, j, [0], [1], [2], [3])
        elif a == 2:
            watch(i, j, [0, 2], [1, 3])
        elif a == 3:
            watch(i, j, [0, 1], [1, 2], [2, 3], [3, 0])
        elif a == 4:
            watch(i, j, [0, 1, 2], [1, 2, 3], [2, 3, 0], [3, 0, 1])
        elif a == 5:
            watch(i, j, [0, 1, 2, 3])

cctvs = list(watch_spot.keys())

sol(0, set(), len(cctvs))
print(blind)

ํ›„๊ธฐ

๊ตฌํ˜„ ๋ฌธ์ œ๋ฅผ ํ’€๋‹ค๋ณด๋‹ˆ ๋ฌธ์ œ๊ฐ€ ์›ํ•˜๋Š” ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ˆœ์„œ๋Œ€๋กœ ์ •ํ™•ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜์ง€๋งŒ ์ตœ์ดˆ ํ•œ ๋ฒˆ๋งŒ ๊ตฌํ•˜๊ณ  ๊ฐ–๋‹ค ์“ธ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„๋“ค์— ๋Œ€ํ•ด์„œ๋Š” ๊ณ ๋ ค๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒŒ ์‹œ๊ฐ„์„ ํ›จ์”ฌ ๋‹จ์ถ•์‹œํ‚จ๋‹ค๋Š” ๊ฑธ ์•Œ์•˜๋‹น
์ฒ˜์Œ ํ•œ ๋ฒˆ ๊ตฌํ•ด๋†“๊ณ  ๊ฐ€์ ธ๋‹ค์“ฐ๋Š”๊ฒŒ ๋งค๋ฒˆ ๊ตฌํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฅผ ๊ฒƒ ๊ฐ™๋‹ค๋ฉด ๊ทธ๋ ‡๊ฒŒ๋„ ์ƒ๊ฐํ•ด๋ณด๋Š” ์—ฐ์Šต์„ ํ•ด๋ด์•ผํ•  ๊ฑฐ ๊ฐ™๋‹ค.

profile
์•„๋ฌด๋ง์ด๋‚˜ํ•˜๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€