-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path15686_치킨 배달.py
More file actions
40 lines (37 loc) · 902 Bytes
/
15686_치킨 배달.py
File metadata and controls
40 lines (37 loc) · 902 Bytes
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
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
ary = [list(map(int,input().split())) for _ in range(n)]
chick=[]
result=[5001]
house = []
for i in range(n):
for j in range(n):
if ary[i][j]==2:
chick.append([i,j])
elif ary[i][j]==1:
house.append([i,j])
def dfs(idx,cnt,loc):
if cnt == m:
calc(loc)
return
for i in range(idx,len(chick)):
if not loc[i]:
loc[i]=1
dfs(i,cnt+1,loc)
loc[i]=0
def calc(loc):
sum = 0
resMin = min(result)
for i,j in house:
value = 101
for k in range(len(chick)):
if loc[k]:
x,y=chick[k]
value = min(value,abs(i-x)+abs(j-y))
sum+=value
if sum > resMin:
return
result.append(sum)
dfs(0,0,[0 for _ in range(len(chick))])
print(min(result))