Post

๐Ÿข ๊ฐ์‹œ

1. ๋ฌธ์ œ ๋งํฌ

15683๋ฒˆ: ๊ฐ์‹œ


2. ์ฝ”๋“œ

Python3 31708KB 2852ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
[15683] ๊ฐ์‹œ

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

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

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

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

์ง€๋„์—์„œ 0์€ ๋นˆ ์นธ, 6์€ ๋ฒฝ, 1~5๋Š” CCTV์˜ ๋ฒˆํ˜ธ์ด๋‹ค.

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

๐Ÿ’š ์ž…๋ ฅ
์ฒซ์งธ ์ค„์— ์‚ฌ๋ฌด์‹ค์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N, M โ‰ค 8)
๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์‚ฌ๋ฌด์‹ค ๊ฐ ์นธ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ์นธ, 6์€ ๋ฒฝ, 1~5๋Š” CCTV๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , ๋ฌธ์ œ์—์„œ ์„ค๋ช…ํ•œ CCTV์˜ ์ข…๋ฅ˜์ด๋‹ค.
CCTV์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 8๊ฐœ๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ’™ ์ถœ๋ ฅ
์ฒซ์งธ ์ค„์— ์‚ฌ๊ฐ ์ง€๋Œ€์˜ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
"""

import copy

# move = [
#     [],
#     [(0, 1), (0, -1), (1, 0), (-1, 0)],
#     [[(0, 1), (0, -1)], [(1, 0), (-1, 0)]],
#     [[(0, 1), (-1, 0)], [(0, 1), (1, 0)], [(0, -1), (1, 0)], [(0, -1), (-1, 0)]],
#     [[(0, 1), (0, -1), (-1, 0)], [(0, 1), (-1, 0), (1, 0)],
#      [(0, 1), (0, -1), (1, 0)], [(0, -1), (-1, 0), (1, 0)]],
#     [(0, 1), (0, -1), (1, 0), (-1, 0)]
# ]

# ์„ธ๋กœ, ๊ฐ€๋กœ
n, m = map(int, input().split())

# cctv ์ด๋™๊ฒฝ๋กœ
move = [
    [],
    [[0], [1], [2], [3]],
    [[0, 2], [1, 3]],
    [[0, 1], [1, 2], [2, 3], [3, 0]],
    [[0, 1, 2], [1, 2, 3], [2, 3, 0], [3, 0, 1]],
    [[0, 1, 2, 3]]
]

# ๋ถ ๋™ ๋‚จ ์„œ
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

cctv = []
office = []
# ์‚ฌ๋ฌด์‹ค ๊ฐ ์นธ์˜ ์ •๋ณด
for i in range(n):
    # [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 6, 0], [0, 0, 0, 0, 0, 0]]
    office.append(list(map(int, input().split())))
    for j in range(m):
        if office[i][j] in [1, 2, 3, 4, 5]:
            # cctv ์ข…๋ฅ˜, x, y ์ขŒํ‘œ
            # [[1, 2, 2]]
            cctv.append([office[i][j], i, j])

# 4๋ฐฉํ–ฅ์— ๋นˆ์นธ ์žˆ๋Š”์ง€
# office ์ •๋ณด, cctv ๋ฐฉํ–ฅ ์ •๋ณด(list ๋กœ ์ฃผ์–ด์ง), cctv ์œ„์น˜


def check(graph, move, x, y):
    for i in move:
        nx = x
        ny = y

        while True:
            nx += dx[i]
            ny += dy[i]

            # ๋ฒ”์œ„ ๋ฒ—์–ด๋‚จ
            if 0 > nx or 0 > ny or nx >= n or ny >= m:
                break
            # ๋ฒฝ
            if graph[nx][ny] == 6:
                break
            # ๊ฐ์‹œ
            elif graph[nx][ny] == 0:
                # ๊ฑ ์Œ์ˆ˜ ๋งŒ๋“ค์–ด๋ฒ„๋ ค~~ ์–ด์ฐจํ”ผ ๊ฐ์‹œ ๋ชปํ•œ ๊ณณ๋งŒ ์…€๊ฑฐ์ž„
                graph[nx][ny] -= -1

# cctv ๊ฐœ์ˆ˜ ๋งŒํผ ๊ตฌํ˜„


def dfs(d, graph):
    global min_value

    # ๊ณ„์† ์ฆ๊ฐ€ํ•˜๋ฉฐ ํ™•์ธํ–ˆ๋˜ cctv๋ฅผ ๋๊นŒ์ง€ ํ–ˆ์„ ๋•Œ
    if d == len(cctv):
        cnt = 0
        for i in range(n):
            # ๊ฐ์‹œ ๋ชปํ•œ ๋นˆ ๊ณต๊ฐ„ 0 ๋งŒ ์„ธ๊ธฐ
            cnt += graph[i].count(0)
        min_value = min(min_value, cnt)
        return

    temp = copy.deepcopy(graph)
    # cctv ๋ฒˆํ˜ธ, cctv ์œ„์น˜
    cctv_num, x, y = cctv[d]

    for i in move[cctv_num]:
        check(temp, i, x, y)
        # ๋‹ค์Œ cctv๋กœ ๋„˜์–ด๊ฐ€๊ณ , cctv ์ •๋ณด ๊ฐฑ์‹  & ๋๋‚ฌ์œผ๋ฉด return
        dfs(d + 1, temp)
        temp = copy.deepcopy(graph)


# ์ตœ๋Œ“๊ฐ’ ํ•ด์„œ ์ตœ์†Œ ์ˆ˜ ๋งŒ๋“ค๊ธฐ
min_value = int(1e9)
dfs(0, office)
print(min_value)


3. ํ•ด์„ค

  • ์ด๋™ ๊ฒฝ๋กœ๋ฅผ ์ˆซ์ž๋กœ ์ง€์ •ํ–ˆ๋‹ค. ํ—ท๊ฐˆ๋ ธ๋‹ค.. ์ฒ˜์Œ์— ์ขŒํ‘œ๋กœ ํ–ˆ๋‹ค๊ฐ€ ๋‚˜์ค‘์— ๋’ค์—์„œ ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ์ง€์ ธ๋ถ„ํ•ด์„œ ๋ฐ”๊ฟจ๋‹ค
  • ๊ฐ์‹œ๋œ ๊ณณ์ด ์•„๋‹Œ ๊ฐ์‹œ๋˜์ง€ ์•Š์€ ๊ณณ์„ ์ฐพ๋Š” ๊ณณ์ด๋ผ 0๋งŒ count ๋˜๊ฒŒ ํ•˜์˜€๋‹ค. count(0) ์ด๋ผ๋Š” ์นœ๊ตฌ๋ฅผ ๋‹ค์‹œ ์ƒ๊ธฐ์‹œํ‚ค๋ฉฐ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์•˜๋‹ค.
  • ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด์„ ๋ญ๋กœ ํ•ด์•ผ๋˜๋Š”์ง€, ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ญ๋กœ ํ•ด์•ผ๋˜๋Š”์ง€ ์‹ ๊ฒฝ์„ ์ข€ ์จ์•ผ ํ–ˆ์—ˆ๋‹ค.
This post is licensed under CC BY 4.0 by the author.