728x90
https://www.acmicpc.net/problem/15686
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 | #include <stdio.h> #include <algorithm> #include <vector> using namespace std; struct POST{ int y, x; }; int n, m, type, ret; vector<POST> house, shop, pick; void dfs(int pos){ if(pick.size() == m){ int candi = 0; for(int i = 0; i < house.size(); i++){ int min_dist = 10000000; for(int j = 0; j < pick.size(); j++){ min_dist = min(min_dist, abs(house[i].y - pick[j].y) + abs(house[i].x - pick[j].x)); } candi += min_dist; } if(ret > candi){ ret = candi; } return ; } for(int i = pos; i < shop.size(); i++){ pick.push_back(shop[i]); dfs(i+1); pick.pop_back(); } } int main(){ POST target; scanf("%d %d", &n, &m); for(int y = 0; y < n; y++){ for(int x = 0; x < n; x++){ scanf("%d", &type); if(type == 1){ target.y = y, target.x = x; house.push_back(target); } if(type == 2){ target.y = y, target.x = x; shop.push_back(target); } } } ret = 0xfffffff; dfs(0); printf("%d\n", ret); return 0; } | cs |
728x90
'Algorithm Study > BOJ with C++' 카테고리의 다른 글
(BAEKJOON) 16235번: 나무 재테크 (0) | 2020.07.08 |
---|---|
(BAEKJOON) 16243번: 인구 이동 (0) | 2020.07.08 |
(BAEKJOON) 15684번: 사다리 조작 (0) | 2020.07.08 |
(BAEKJOON) 15683번: 감시 (0) | 2020.07.08 |
(BAEKJOON) 13460번: 구슬 탈출2 (0) | 2020.07.08 |
댓글