๊ฐ cctv์ ๋ฐฉํฅ์ ๋ฐ๋ผ ๊ฐ์ ์ง์ญ์ ์ค์ ํ๊ณ , ๊ฐ์ ๋์ง ์๋ ์ฌ๊ฐ์ง๋์ ์ต์๊ฐ์ ๊ตฌํ๋ ๋ฌธ์
๋ฐฑ์ค ์ฐ๊ตฌ์ ์๋ฆฌ์ฆ๋ ์ข ๋น์ทํ๋ค๊ณ ๋๊ผ๋ค.
- cctv์ ๋ฒํธ์ ์์น๋ฅผ ๊ตฌํ๋ค
- ๊ฐ cctv๋ฅผ ๋๋ฉด์ ํ๋์ฉ ๋ฐฉํฅ์ ๋ฐ๋ผ ๊ฐ์์ง์ญ์ ์ค์ ํ๋ค. ์ฒ์์ ๋ถ์ชฝ ๋ฐฉํฅ, ๊ทธ ๋ค์์ ๋จ์ชฝ ๋ฐฉํฅ ๋ฑ๋ฑ...
- 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(๊ฐ์ ์ง์ญ ์ ๋ณด)๋ค์ ๊ฐ์ง๊ณ ์ฌ๊ฐ์ง๋์ ๊ฐ์๋ฅผ ๊ตฌํ ์ ์๋ ๊ฒ.
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
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])
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)
๊ตฌํ ๋ฌธ์ ๋ฅผ ํ๋ค๋ณด๋ ๋ฌธ์ ๊ฐ ์ํ๋ ์๊ตฌ ์ฌํญ์ ์์๋๋ก ์ ํํ๊ฒ ๊ตฌํํ๋ ๊ฒ๋ ์ค์ํ์ง๋ง ์ต์ด ํ ๋ฒ๋ง ๊ตฌํ๊ณ ๊ฐ๋ค ์ธ ์ ์๋ ๋ถ๋ถ๋ค์ ๋ํด์๋ ๊ณ ๋ ค๋ฅผ ํด์ฃผ๋ ๊ฒ ์๊ฐ์ ํจ์ฌ ๋จ์ถ์ํจ๋ค๋ ๊ฑธ ์์๋น
์ฒ์ ํ ๋ฒ ๊ตฌํด๋๊ณ ๊ฐ์ ธ๋ค์ฐ๋๊ฒ ๋งค๋ฒ ๊ตฌํ๋ ๊ฒ๋ณด๋ค ํจ์ฌ ๋น ๋ฅผ ๊ฒ ๊ฐ๋ค๋ฉด ๊ทธ๋ ๊ฒ๋ ์๊ฐํด๋ณด๋ ์ฐ์ต์ ํด๋ด์ผํ ๊ฑฐ ๊ฐ๋ค.