๋ฌธ์
https://www.acmicpc.net/problem/15683
ํ์ด
# ๋ฐฑ์ค 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
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Silver4] 11652๋ฒ_์นด๋ (0) | 2023.04.10 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Silver5] 11004๋ฒ_K๋ฒ์งธ ์ (0) | 2023.04.09 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver3] 1213๋ฒ_ํฐ๋ฆฐ๋๋กฌ ๋ง๋ค๊ธฐ (0) | 2023.04.04 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 1976๋ฒ_์ฌํ ๊ฐ์ (1) | 2023.04.03 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 9935๋ฒ_๋ฌธ์์ด ํญ๋ฐ (0) | 2023.04.03 |