-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1035_조각_움직이기.py
56 lines (46 loc) · 1.41 KB
/
1035_조각_움직이기.py
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
from collections import deque
from itertools import combinations, permutations
import sys
def input(): return sys.stdin.readline().strip()
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
def isValid(indices):
remain = set(indices)
queue = deque([indices[0]])
while queue:
here = queue.popleft()
y, x = divmod(here, N)
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if nx < 0 or nx >= N or ny < 0 or ny >= N:
continue
if ny * N + nx not in remain:
continue
remain.remove(ny * N + nx)
queue.append(ny * N + nx)
return len(remain) == 0
def getLength(indices):
retval = float('inf')
for order in permutations(range(count), count):
temp = 0
for i in range(count):
temp = temp + abs(arr[i] // N - indices[order[i]] // N) + abs(arr[i] % N - indices[order[i]] % N)
retval = min(retval, temp)
return retval
N = 5
data = [list(map(lambda x: x == '*', input())) for _ in range(5)]
count = sum(map(lambda x: x.count(True), data))
if count == 1:
print(0)
exit()
arr = []
for i in range(N):
for j in range(N):
if data[i][j]:
arr.append(i * N + j)
answer = float('inf')
for indices in combinations(list(range(N*N)), count):
if not isValid(indices):
continue
answer = min(answer, getLength(indices))
print(answer)