해보자

백준_15686번_치킨 배달 본문

C++/Solve & Think

백준_15686번_치킨 배달

안댕 2020. 10. 8. 01:25


소스코드

#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>

using namespace std;
vector<pair<int, int>> house;
vector<pair<int, int>> chicken;

int result = INT_MAX;
int N, M;

int calcDistance(vector<pair<int, int>> t_chicken) {    
    int total = 0;
    for (int i = 0; i < house.size(); i++) {
        int distance = INT_MAX;
        for (int j = 0; j < t_chicken.size(); j++) {
            distance = min(distance,
                abs(t_chicken[j].first - house[i].first) + abs(t_chicken[j].second - house[i].second));
        }
        total += distance;
    }
    return total;
}

void dfs(int idx, vector<pair<int, int>> t_chicken) {
    if (t_chicken.size() == M) {
        result = min(calcDistance(t_chicken), result);
        return;
    }

    for (int start = idx; start < chicken.size(); start++) {
        t_chicken.push_back(chicken[start]);
        dfs(start + 1, t_chicken);
        t_chicken.pop_back();
    }
}
int main() {
    int t;
    cin >> N >> M;
    vector<pair<int, int>> t_chicken;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cin >> t;
            if ( t == 1)
                house.push_back(make_pair(i,j));
            if ( t == 2)
                chicken.push_back(make_pair(i, j));
        }
    }
    dfs(0, t_chicken);

    cout << result << endl;
    return 0;
}

'C++ > Solve & Think' 카테고리의 다른 글

백준_16236번_아기상어  (0) 2020.10.09
백준_16234번_인구 이동  (0) 2020.10.08
백준 14503번 로봇 청소기  (0) 2020.04.19
프로그래머스_LV2_다리를 지나는 트럭  (0) 2020.02.26
프로그래머스_LV2_기능개발  (0) 2020.02.21