-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15686.cpp
More file actions
104 lines (75 loc) · 1.59 KB
/
15686.cpp
File metadata and controls
104 lines (75 loc) · 1.59 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// 15686.cpp
// algo
//
// Created by 박효정 on 2021/09/02.
//
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstring>
using namespace std;
int arr[52][52];
int res=987654321;
vector<string>list;
//치킨
vector<pair<int, int>>c;
//홈
vector<pair<int, int>>h;
vector<pair<int, int>>pos;
int n,m;
//치킨집 수
int num=0;
int cal(){
int sum=0;
for(int i=0;i<h.size();i++){
int tmp=987654321;
int hx=h[i].first;
int hy=h[i].second;
for(int j=0;j<pos.size();j++){
int x=pos[j].first;
int y=pos[j].second;
tmp=min(tmp,abs(x-hx)+abs(y-hy));
}
sum+=tmp;
}
return sum;
}
//치킨 가게 선택 방법 고르기 (list 벡터에 저장)
void dfs(int index,int cnt){
if(index>=num||cnt>=m){
if(cnt==m){
res=min(res,cal());
}
return;
}
else{
pos.push_back({c[index].first,c[index].second});
dfs(index+1,cnt+1);
pos.pop_back();
dfs(index+1, cnt);
}
}
int main(){
cin.tie(0);
cout.tie(0);
std::ios::sync_with_stdio(false);
cin>>n>>m;
//입력 받음
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>arr[i][j];
if(arr[i][j]==1){
h.push_back({i,j});
}else if(arr[i][j]==2){
num++;
c.push_back({i,j});
}
}
}
//최대 m개의 치킨 가게 선택
dfs(0,0);
cout<<res<<"\n";
return 0;
}