ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [15686] 치킨 배달
    BOJ 2021. 11. 5. 00:05

    https://www.acmicpc.net/problem/15686

     

    15686번: 치킨 배달

    크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸

    www.acmicpc.net

    <문제>

    치킨집의 개수를 x라 하면, m개의 1, (x-m)개의 0으로 구성된 배열을 next_permutation으로 모든 경우를 만들어준다.

    치킨집의 위치와 집의 위치를 미리 별도의 자료구조로 저장해둔다면,

    단순히 next_permutation의 결과가 0일때에만 continue하면서 거리를 구해줄 수 있다.

    하나의 집을 기준으로 할때, 가장 가까운 치킨집 하나만 계산해야 하므로 항상 최소값으로 갱신해주고,

    갱신된 최소값 하나만 최종 결과에 누적시켜주어야 한다.

     

    <소스코드>

    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
    #include <bits/stdc++.h>
    using namespace std;
    int n, m, cnt, a[60][60];
    vector<pair<intint>> home;
    vector<pair<intint>> pos;
    int getDist(vector<int> base) {
        int ret = 0;
        for (auto& start : home) {
            int cur = 1e9;
            for (int end = 0end < base.size(); end++) {
                if (base[end== 0continue;
                int score = abs(start.first - pos[end].first) +
                            abs(start.second - pos[end].second);
                cur = min(score, cur);
            }
            ret += cur;
        }
        return ret;
    }
    int main(void) {
        cin >> n >> m;
        int i, j;
        for (i = 0; i < n; i++)
            for (j = 0; j < n; j++) {
                cin >> a[i][j];
                if (a[i][j] == 2) pos.push_back({i, j});
                if (a[i][j] == 1) home.push_back({i, j});
            }
        int S = pos.size();
        vector<int> base(S);
        for (i = 0; i < m; i++) base[i] = 1;
        sort(base.begin(), base.end());
        int ans = 1e9;
        do {
            int score = getDist(base);
            ans = min(score, ans);
        } while (next_permutation(base.begin(), base.end()) == true);
        cout << ans;
        return 0;
    }
    cs

     

    'BOJ' 카테고리의 다른 글

    [10835] 카드게임  (0) 2021.11.06
    [2234] 성곽  (0) 2021.11.05
    [1992] 쿼드트리  (0) 2021.11.04
    [1700] 멀티탭 스케줄링  (0) 2021.11.04
    [13907] 세금  (0) 2021.11.03

    댓글