-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ-13
More file actions
33 lines (30 loc) · 828 Bytes
/
Q-13
File metadata and controls
33 lines (30 loc) · 828 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
from itertools import combinations
n,m=map(int,input().split())
data=[]
for _ in range(n):
data.append(list(map(int,input().split())))
def solution(data,m):
result=[]
house=[]
bbq=[]
for i in range(n):
for j in range(n):
if data[i][j]==1:
house.append((i,j))
for i in range(n):
for j in range(n):
if data[i][j]==2:
bbq.append((i,j))
combination=list(combinations(bbq,m))
def get_sum(house,bbq_m):
sum=0
for x in house:
distance=[]
for y in bbq_m:
distance.append(abs(x[0]-y[0])+abs(x[1]-y[1]))
sum+=min(distance)
return sum
for combi in combination:
result.append(get_sum(house,combi))
return min(result)
print(solution(data,m))