File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed
Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < vector>
3+ #include < algorithm>
4+ #include < cstring>
5+ #include < queue>
6+ using namespace std ;
7+
8+ struct Node {
9+ int x, y;
10+ };
11+
12+ int N, M, answer = 1e9 ;
13+ int arr[51 ][51 ];
14+ bool visited[251 ];
15+
16+ vector<Node> chicken;
17+ vector<Node> home;
18+ vector<int > v;
19+
20+ void combi (int idx){
21+ if (v.size () == M) {
22+ // 치킨집과 집의 거리
23+ int sum = 0 ;
24+ for (int i = 0 ; i < home.size (); i++){
25+ int res = 1e9 ; // 집에서 가장 가까운 치킨집
26+ for (int j = 0 ; j < v.size (); j++){
27+ res = min (res, abs (home[i].x - chicken[v[j]].x ) + abs (home[i].y - chicken[v[j]].y ));
28+ }
29+ sum += res;
30+ }
31+ answer = min (answer, sum);
32+ return ;
33+ }
34+
35+ for (int i = idx; i < chicken.size (); i++){
36+ if (!visited[i]) {
37+ visited[i] = true ;
38+ v.push_back (i);
39+ combi (i);
40+ v.pop_back ();
41+ visited[i] = false ;
42+ }
43+ }
44+ }
45+
46+ int main () {
47+ ios::sync_with_stdio (false );
48+ cin.tie (NULL ); cout.tie (NULL );
49+
50+ cin >> N >> M;
51+ for (int i = 0 ; i < N; i++){
52+ for (int j = 0 ; j < N; j++){
53+ cin >> arr[i][j];
54+ if (arr[i][j] == 1 ) {
55+ home.push_back ({i, j});
56+ } else if (arr[i][j] == 2 ) {
57+ chicken.push_back ({i, j});
58+ }
59+ }
60+ }
61+
62+ // 치킨집 좌표 조합
63+ combi (0 );
64+
65+ cout << answer;
66+ }
You can’t perform that action at this time.
0 commit comments