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
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.