-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210317_BOJ_15686.cpp
More file actions
67 lines (54 loc) · 1.06 KB
/
Copy path210317_BOJ_15686.cpp
File metadata and controls
67 lines (54 loc) · 1.06 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
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
const int INF = 987654321;
int N, M;
vector<pair<int, int>> h;
vector<pair<int, int>> c;
int Min = INF;
void input() {
cin >> N >> M;
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
int info; cin >> info;
if (info == 1) h.push_back({ i, j });
else if (info == 2) c.push_back({ i, j });
}
}
}
void findAnswer(vector<int> t, int pre) {
if (t.size() == M) {
int total = 0;
for (int i = 0; i < h.size(); ++i) {
int a = h[i].first;
int b = h[i].second;
int shortest = INF;
for (int j = 0; j < t.size(); ++j) {
int x = c[t[j]].first;
int y = c[t[j]].second;
int d = abs(a - x) + abs(b - y);
if (shortest > d)
shortest = d;
}
total += shortest;
}
if (Min > total)
Min = total;
return;
}
for (int i = pre + 1; i < c.size(); ++i) {
t.push_back(i);
findAnswer(t, i);
t.pop_back();
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
input();
vector<int> t;
findAnswer(t, -1);
cout << Min << "\n";
return 0;
}