๐ฆ ๊ฐ์
1. ๋ฌธ์ ๋งํฌ
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
stack = []
MAP = []
MIN = 10000
blocked = 0
def backtracking(depth):
global MAP
global MIN
global N, M, D
global stack
global camera
global blocked
if depth == D:
MIN = min(MIN, N * M - blocked - len(stack))
return
x, y = camera[depth]
delta = [[0, 1], [1, 0], [0, -1], [-1, 0], ] #์์ฐํ์ข
if MAP[y][x] == 1:
for dx, dy in delta:
count = doWatch(x, y, [dx, dy])
# ํ์ชฝ์ ํฌ์ํ์ผ๋ ๋ค์ ๋ฐฑํธ๋ํน
backtracking(depth + 1)
# ์์ ๋ณต๊ตฌ
for _ in range(count):
stack.pop()
elif MAP[y][x] == 2:
for i in range(2):
dx, dy = delta[i]
count = doWatch(x, y, [dx, dy])
dx, dy = delta[i + 2]
count += doWatch(x, y, [dx, dy])
# ํ์ชฝ์ ํฌ์ํ์ผ๋ ๋ค์ ๋ฐฑํธ๋ํน
backtracking(depth + 1)
# ์์ ๋ณต๊ตฌ
for _ in range(count):
stack.pop()
elif MAP[y][x] == 3:
for i in range(4):
dx, dy = delta[i]
count = doWatch(x, y, [dx, dy])
dx, dy = delta[(i + 1) % 4]
count += doWatch(x, y, [dx, dy])
# ํ์ชฝ์ ํฌ์ํ์ผ๋ ๋ค์ ๋ฐฑํธ๋ํน
backtracking(depth + 1)
# ์์ ๋ณต๊ตฌ
for _ in range(count):
stack.pop()
elif MAP[y][x] == 4:
for i in range(4):
dx, dy = delta[i]
count = doWatch(x, y, [dx, dy])
dx, dy = delta[(i + 1) % 4]
count += doWatch(x, y, [dx, dy])
dx, dy = delta[(i + 2) % 4]
count += doWatch(x, y, [dx, dy])
# ํ์ชฝ์ ํฌ์ํ์ผ๋ ๋ค์ ๋ฐฑํธ๋ํน
backtracking(depth + 1)
# ์์ ๋ณต๊ตฌ
for _ in range(count):
stack.pop()
elif MAP[y][x] == 5:
dx, dy = delta[0]
count = doWatch(x, y, [dx, dy])
dx, dy = delta[1]
count += doWatch(x, y, [dx, dy])
dx, dy = delta[2]
count += doWatch(x, y, [dx, dy])
dx, dy = delta[3]
count += doWatch(x, y, [dx, dy])
# ํ์ชฝ์ ํฌ์ํ์ผ๋ ๋ค์ ๋ฐฑํธ๋ํน
backtracking(depth + 1)
# ์์ ๋ณต๊ตฌ
for _ in range(count):
stack.pop()
def doWatch(x, y, delta):
global MAP
global stack
global N, M
global camera
dx, dy = delta
nx = x
ny = y
count = 0
while True:
nx = nx + dx
ny = ny + dy
if 0 <= nx < M and 0 <= ny < N:
# ๋ฒฝ์ ๋ง์ฃผํจ
if MAP[ny][nx] == 6:
break
# ์ด๋ฏธ ๊ฒ์ฌํ ๊ณณ ํต๊ณผ
if [nx, ny] in stack or [nx, ny] in camera:
continue
# ๊ฒ์ฌํ ๊ณณ ์ ์ฅ
stack.append([nx, ny])
count += 1
else:
break
return count
camera = []
N, M = map(int, input().split())
for i in range(N):
MAP.append(list(map(int, input().split())))
for j in range(M):
if 1 <= MAP[i][j] <= 5:
camera.append([j, i])
blocked += 1
if MAP[i][j] == 6:
blocked += 1
D = len(camera)
backtracking(0)
print(MIN)
3. ํด์ค
python3
31120kb
520ms
์ฝ๊ฐ์ ๋ ธ๊ฐ๋ค์ฑ ๋ฐฑํธ๋ํน ๋ฌธ์
1๋ฒ๋ถํฐ 5๋ฒ ์ ํ์ ์นด๋ฉ๋ผ ์์น๋ฅผ ๊ธฐ์ตํ๊ณ while ๋ฌธ์ ๋๋ ค์ ๋ฒฝ์ ๋ฟ์๋ ๊น์ง ์คํ์ ๊ฐ์ํ ์์น๋ฅผ ์ ์ฅ
์ด๋ ์นด๋ฉ๋ผ์ ์ด๋ฏธ ๊ฒ์ฌํ ์ง์ ์ ๋ฐฉ๋ฌธ ์ฒดํฌ (stack) ๋ฅผ ํตํด ๊ธฐ์ตํ๊ณ ์ ์ฅํ์ง ์์
์นด๋ฉ๋ผ์ ๊ฐ์์ depth ๊ฐ ์ผ์นํ๋ฉด ๋ฐฑํธ๋ํน ์ข ๋ฃ
์ดํ ์ฌ๊ท๋ฅผ ๋น ์ ธ๋์ฌ ๋ ๊ฐ์ ์์ํ ์ง์ ์ ๊ฐ์ ๋งํผ stack ์์ pop ํด์ฃผ์ด ์๋ณต ์ํด
This post is licensed under CC BY 4.0 by the author.