๐ฆ ํ๊ทธ ์๋ฆฌํ ์ด
1. ๋ฌธ์ ๋งํฌ
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.