-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15686.cpp
More file actions
73 lines (61 loc) · 1.73 KB
/
15686.cpp
File metadata and controls
73 lines (61 loc) · 1.73 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
#include <iostream>
#include <vector>
#include <cmath> // abs
#include <algorithm> // min
#include <climits> // INT_MAX
using namespace std;
int N, M;
vector<pair<int, int> > home;
vector<pair<int, int> > chicken;
vector<vector<pair<int, int> > > combinations;
void combination(vector<pair<int, int> >& current, int start, int K) {
if(current.size() == K) {
combinations.push_back(current);
return;
}
for(int i=start;i<chicken.size();i++) {
current.push_back(chicken[i]);
combination(current, i+1, K);
current.pop_back();
}
}
int calcul(vector<pair<int, int> >& current) {
int total = 0;
for(int i=0;i<home.size();i++) {
int min_v = INT_MAX;
for(int j=0;j<current.size();j++) {
// cout << current[j].first << ", " << current[j].second << "\n";
min_v = min(abs(home[i].first-current[j].first) + abs(home[i].second-current[j].second), min_v);
}
total += min_v;
}
return total;
}
int main()
{
cin >> N >> M;
vector<vector<int> > S(N, vector<int>(N));
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
cin >> S[i][j];
if(S[i][j] == 1) {
pair<int, int> a;
a = make_pair(i, j);
home.push_back(a);
} else if(S[i][j] == 2) {
pair<int, int> a;
a = make_pair(i, j);
chicken.push_back(a);
}
}
}
vector<pair<int, int> > backtracking;
for(int i=1;i<=M;i++) {
combination(backtracking, 0, i);
}
int result = INT_MAX;
for(int i=0;i<combinations.size();i++) {
result = min(calcul(combinations[i]), result);
}
cout << result;
}