forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path7.cpp
More file actions
62 lines (53 loc) Β· 1.76 KB
/
7.cpp
File metadata and controls
62 lines (53 loc) Β· 1.76 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
#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[50][50];
vector<pair<int, int> > chicken;
vector<pair<int, int> > house;
// μΉν¨ 거리μ ν©μ κ³μ°νλ ν¨μ
int getSum(vector<pair<int, int> > candidates) {
int result = 0;
// λͺ¨λ μ§μ λνμ¬
for (int i = 0; i < house.size(); i++) {
int hx = house[i].first;
int hy = house[i].second;
// κ°μ₯ κ°κΉμ΄ μΉν¨ μ§μ μ°ΎκΈ°
int temp = 1e9;
for (int j = 0; j < candidates.size(); j++) {
int cx = candidates[j].first;
int cy = candidates[j].second;
temp = min(temp, abs(hx - cx) + abs(hy - cy));
}
// κ°μ₯ κ°κΉμ΄ μΉν¨ μ§κΉμ§μ 거리λ₯Ό λνκΈ°
result += temp;
}
// μΉν¨ 거리μ ν© λ°ν
return result;
}
int main(void) {
cin >> n >> m;
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
cin >> arr[r][c];
if (arr[r][c] == 1) house.push_back({r, c}); // μΌλ° μ§
else if (arr[r][c] == 2) chicken.push_back({r, c}); // μΉν¨μ§
}
}
// λͺ¨λ μΉν¨ μ§ μ€μμ mκ°μ μΉν¨ μ§μ λ½λ μ‘°ν© κ³μ°
vector<bool> binary(chicken.size());
fill(binary.end() - m, binary.end(), true);
// μΉν¨ 거리μ ν©μ μ΅μλ₯Ό μ°Ύμ μΆλ ₯
int result = 1e9;
do {
vector<pair<int, int> > now;
for (int i = 0; i < chicken.size(); i++) {
if (binary[i]) {
int cx = chicken[i].first;
int cy = chicken[i].second;
now.push_back({cx, cy});
}
}
result = min(result, getSum(now));
} while(next_permutation(binary.begin(), binary.end()));
cout << result << '\n';
}