Post

๐ŸฆŠ ํŽ˜๊ทธ ์†”๋ฆฌํ…Œ์–ด

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

9207๋ฒˆ: ํŽ˜๊ทธ ์†”๋ฆฌํ…Œ์–ด


2. ์ฝ”๋“œ

Python3 31120KB 140ms

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
delta = [
    (0,1), (0,-1), (1,0), (-1,0)
]

ROW = 5
COLUMN = 9

N = int(input())

MIN_PIN = 2000000000
MIN_MOVE = 2000000000

def backtracking(move: int):
    global MIN_PIN
    global MIN_MOVE
    # ํƒˆ์ถœ์กฐ๊ฑด
    flag = False

    # ๊ฒ€์‚ฌ
    for i in range(ROW):
        for j in range(COLUMN):
            if MAP[i][j] == 'o':
                for dr, dc in delta:
                    nr = i + dr
                    nc = j + dc
                    if 0 <= nr < ROW and 0 <= nc < COLUMN and MAP[nr][nc] == 'o':
                        # (nr, nc) not in no_hole and
                        # ๊ตฌ๋ฉ์„ ๋›ฐ์–ด๋„˜์–ด ๋‹ค๋ฅธ ์นธ์œผ๋กœ ์ด๋™
                        nnr = nr + dr
                        nnc = nc + dc
                        if 0 <= nnr < ROW and 0 <= nnc < COLUMN and MAP[nnr][nnc] == '.':
                            flag = True
                            # ๋ฐฑํŠธ๋ž˜ํ‚น
                            MAP[i][j] = '.'
                            MAP[nr][nc] = '.'
                            MAP[nnr][nnc] = 'o'
                            backtracking(move + 1)
                            # ๋ณต๊ท€
                            MAP[i][j] = 'o'
                            MAP[nr][nc] = 'o'
                            MAP[nnr][nnc] = '.'

    # ๋ชจ๋“  ์นธ์—์„œ ์ด๋™์ด ๋ถˆ๊ฐ€ํ•˜๋ฉด
    if not flag:
        pin = 0
        for i in range(ROW):
            for j in range(COLUMN):
                if MAP[i][j] == 'o':
                    pin += 1
        if pin < MIN_PIN:
            MIN_MOVE = move
            MIN_PIN = pin

for n in range(N):
    MIN_PIN = 2000000000
    MIN_MOVE = 2000000000
    MAP = []
    for i in range(ROW):
        MAP.append(list(input()))

    backtracking(0)

    print(MIN_PIN, MIN_MOVE)

    if n < N - 1:
        input()


3. ํ•ด์„ค

์ธ์ ‘ํ•œ ์นธ โ‡’ ํ•ด๋‹น ํ•€์˜ ์ƒํ•˜์ขŒ์šฐ์— ํ•ด๋‹น๋˜๋Š” ํ•€ ์ด๋ฅผ ๋›ฐ์–ด๋„˜์–ด ๊ตฌ๋ฉ์ด ์žˆ๋Š” ์นธ์ผ ๊ฒฝ์šฐ ๋ฐฑํŠธ๋ž˜ํ‚น ์‹œํ–‰ flag ๋กœ ๋ชจ๋“  ์นธ์— ๋Œ€ํ•ด ์ด๋™์„ 1๋ฒˆ์ด๋ผ๋„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•ด์„œ, ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ชจ๋“  ๋งต์˜ ํ•€์„ ๊ฒ€์‚ฌ ํ•€์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ์†Œ์ผ ๊ฒฝ์šฐ ์ด๋™ํšŸ์ˆ˜ ๊ฐฑ์‹ 

This post is licensed under CC BY 4.0 by the author.