알고리즘 문제/BOJ
15686번 치킨 배달
parkit
2018. 10. 8. 23:47
728x90
반응형
https://www.acmicpc.net/problem/15686
vector로 하려고 했으나, C언어스럽게(?) 구현하고 싶었다.
house_count, chicken_count, Index를 vector의 push와 pop으로 생각하면 된다.
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> using namespace std; typedef struct house // 집 구조체 { int y; int x; }house; typedef struct chicken // 치킨 구조체 { int y; int x; }chicken; typedef struct live // 살아남는 치킨 집을 담을 구조체 { int y; int x; }live; house h[102]; chicken c[14]; live l[14]; int N = 0, M = 0; int Map[51][51] = { 0, }; int chicken_count = -1; int house_count = -1; int answer = 987654321; int Index = -1; int DFS(int cnt) { if (Index + 1 == M) { int sum = 0; for (int i = 0; i < house_count + 1; i++) { int hy = h[i].y; int hx = h[i].x; int MIN = 987654321; for (int j = 0; j < M; j++) { int s = abs(hy - l[j].y) + abs(hx - l[j].x); if (MIN > s) MIN = s; } sum += MIN; } return sum; } for (int i = cnt; i < chicken_count + 1; i++) { ++Index; l[Index].y = c[i].y; l[Index].x = c[i].x; int Compare = DFS(i + 1); if (answer > Compare) { answer = Compare; } --Index; } return answer; } int main(void) { scanf("%d %d", &N, &M); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &Map[i][j]); if (Map[i][j] == 1) { ++house_count; h[house_count].y = i; h[house_count].x = j; } else if (Map[i][j] == 2) { ++chicken_count; c[chicken_count].y = i; c[chicken_count].x = j; } } } printf("%d\n", DFS(0)); return 0; } | cs |
728x90
반응형