[BaekJoon] 15683λ² : κ°μ
Updated:
15683λ² : κ°μ
λ무λ λΉμ°νκ² κ°μ λ²μκ° λμ CCTV μμΌλ‘ κ°μν μ μλ μ΅λ λ²μλ₯Ό κ°μν΄κ°λ©° κ³μ°μ νλλ° λ°λ‘ νλ Έλ€.
무쑰건 μ΅λ λ²μλ₯Ό κ°μνλ κ² μ³μ λ°©λ²μ΄ μλμλ κ²μ΄λ€.
κ²°κ΅ CCTVκ° κ°μν μ μλ λͺ¨λ κ²½μ°λ₯Ό λ°μ Έ κ·Έμ€ μ¬κ°μ§λκ° μ΅μμΌ λλ₯Ό λ°νν΄μΌ νλ€.
λ§μ½ 1λ², 2λ² CCTVκ° κ°κ° ν λμ© μμλ€κ³ νλ©΄ μ΄ 8 κ°μ§μ κ²½μ°κ° λμ¨λ€.
1λ²μ΄ μμͺ½μ κ°μνλ©° 2λ²μ΄ μ’ μ°, μ ν μΈ κ²½μ°
1λ²μ΄ μ€λ₯Έμͺ½μ κ°μνλ©° 2λ²μ΄ μ’ μ°, μ ν μΈ κ²½μ°
1λ²μ΄ μλμͺ½μ κ°μνλ©° 2λ²μ΄ μ’ μ°, μ ν μΈ κ²½μ°
1λ²μ΄ μΌμͺ½μ κ°μνλ©° 2λ²μ΄ μ’ μ°, μ ν μΈ κ²½μ°
κ·Έλ°λ°.. νμ΄ λ°©λ²μ μμλλ° μ΄κ±Έ μ½λλ‘ λ°κΎΈμλ λμ ν λͺ¨λ₯΄κ² μ΄μ λ€λ₯Έ μ¬λμ μ½λλ₯Ό μ°Έκ³ νλ€
https://qrlagusdn.tistory.com/150
λ΄κ° μκ°νλ νμ΄ λ°©λ²μ μ½λλ‘ λ³΄λ λ°°μΈ μ μ΄ μ λ§ λ§μ΄ μμλ€.
νΉν CCTVκ° λ°λΌλ³΄λ λͺ¨λ κ²½μ°λ₯Ό μ¬κ·λ‘ ν΄κ²°ν λΆλΆμ΄ μΈμ κΉμλ€.
μ²μμ μ΄ν΄κ° μκ° λλ²κΉ μ μμ² νλ€..
λ°°μ΄ κ² λ§μ΄ μλ λ¬Έμ μλ€ μκ°μ΄ μ§λκ³ λ€μ κΌ νμ΄μΌ ν λ¬Έμ 1μμμ΄λ€.
import copy
from collections import Counter
# μ, ν, μ’, μ°
dir_i = [-1, 1, 0, 0]
dir_j = [0, 0, -1, 1]
detection_dir = [0, [[0], [1], [2], [3]], [[0, 1], [2, 3]], [[1, 2], [1, 3], [0, 2], [0, 3]],
[[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]], [[0, 1, 2, 3]]]
MIN = float("INF")
def solution(cctv_idx, office, cctv_list):
global MIN
if cctv_idx == len(cctv_list):
detection_area = Counter(sum(office, []))[0]
MIN = min(MIN, detection_area)
return
cctv_num, cctv_i, cctv_j = cctv_list[cctv_idx]
for cctv_dir in detection_dir[cctv_num]:
tmp_office = copy.deepcopy(office)
for i in cctv_dir:
visit_i = cctv_i + dir_i[i]
visit_j = cctv_j + dir_j[i]
while 0 <= visit_i < n and 0 <= visit_j < m:
if tmp_office[visit_i][visit_j] == 6:
break
elif tmp_office[visit_i][visit_j] == 0:
tmp_office[visit_i][visit_j] = 9
visit_i += dir_i[i]
visit_j += dir_j[i]
solution(cctv_idx + 1, tmp_office, cctv_list)
if __name__ == "__main__":
n, m = map(int, input().rsplit())
office = [list(map(int, input().rsplit())) for _ in range(n)]
cctv_list = []
for i in range(n):
for j in range(m):
if office[i][j] not in [0, 6]:
cctv_list.append([office[i][j], i, j])
solution(0, office, cctv_list)
print(MIN)
Leave a comment