https://www.acmicpc.net/problem/14889
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 22;
int n;
int answer = 987654321;
int map[MAX][MAX];
vector<int> team;
void CalAns() {
vector<int> fteam, steam;
for (int i = 0; i < team.size(); i++) {
if (team[i] == 0) fteam.push_back(i);
else steam.push_back(i);
}
if (fteam.size() != steam.size()) return;
int tmp1 = 0;
int tmp2 = 0;
for (int i = 0; i < fteam.size(); i++) {
for (int j = 0; j < fteam.size(); j++) {
tmp1 += map[fteam[i]][fteam[j]];
tmp2 += map[steam[i]][steam[j]];
}
}
answer = min(answer, abs(tmp1 - tmp2));
}
void MakeTeam(int num1,int num2) {
if (team.size() == n) {
CalAns();
return;
}
for (int i = 0; i <= 1; i++) {
if (i == 0 && num1 <n/2) {
team.push_back(i);
MakeTeam(num1 + 1, num2);
team.pop_back();
}
else if (i == 1 && num2 < n / 2) {
team.push_back(i);
MakeTeam(num1, num2 + 1);
team.pop_back();
}
}
}
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
}
}
MakeTeam(0,0);
cout << answer << endl;
}
'백준 알고리즘 > 구현' 카테고리의 다른 글
어항 정리 C++ (0) | 2022.01.22 |
---|---|
마법사 상어와 복제 (0) | 2021.12.03 |
스타트와 링크 C++ (0) | 2021.10.22 |
경사로 C++ (0) | 2021.10.22 |
감시 C++ (0) | 2021.10.22 |