
https://www.acmicpc.net/problem/14889
const filePath = process.platform === "linux" ? "dev/stdin" : "./input.txt";
const input = require("fs")
.readFileSync(filePath)
.toString()
.trim()
.split("\n");
const N = Number(input.shift());
const ability = input.map((el) => el.split(" ").map(Number));
function getCombinations(arr, selectNum) {
if (selectNum === 1) return arr.map((el) => [el]);
const result = [];
arr.forEach((fixed, idx, origin) => {
const rest = origin.slice(idx + 1);
const combinations = getCombinations(rest, selectNum - 1);
const attached = combinations.map((combination) => [
fixed,
...combination,
]);
result.push(...attached);
});
return result;
}
function getPermutations(arr, selectNum) {
if (selectNum === 1) return arr.map((el) => [el]);
const result = [];
arr.forEach((fixed, idx, origin) => {
const rest = [...origin.slice(0, idx), ...origin.slice(idx + 1)];
const permutations = getPermutations(rest, selectNum - 1);
const attached = permutations.map((permutation) => [
fixed,
...permutation,
]);
result.push(...attached);
});
return result;
}
const combinations = getCombinations(
new Array(N).fill().map((_, idx) => idx),
N / 2
);
let min = 10000000;
for (let i = 0; i < combinations.length; i++) {
let start = 0;
let link = 0;
// 스타트팀
for (const [a, b] of getPermutations(combinations[i], 2)) {
start += ability[a][b];
}
const linkTeam = new Array(N)
.fill()
.map((_, idx) => idx)
.filter((el) => !combinations[i].includes(el));
// 링크팀
for (const [a, b] of getPermutations(linkTeam, 2)) {
link += ability[a][b];
}
min = Math.min(min, Math.abs(start - link));
}
console.log(min);
function carculateTeamAbility(team) {
let result = 0;
for (const [a, b] of getPermutations(team, 2)) {
result += ability[a][b];
}
return result;
}
... 중략
const start = carculateTeamAbility(combinations[i]);
const link = carculateTeamAbility(linkTeam);
function carculateTeamAbility(team) {
let result = 0;
for (let i = 0; i < team.length; i++) {
for (let j = 0; j < team.length; j++) {
result += ability[team[i]][team[j]];
}
}
return result;
}

실제로 실행시간을 많이 줄였다 !!
백트래킹 단계의 문제를 풀면서 백트래킹을 사용하지 않았다.. 그래서 백트래킹을 이용한 풀이를 찾아보며(아직 익숙하지 않다..) 백트래킹 공부를 해보자.
const filePath = process.platform === "linux" ? "dev/stdin" : "./input.txt";
const input = require("fs")
.readFileSync(filePath)
.toString()
.trim()
.split("\n");
const N = Number(input.shift());
const ability = input.map((line) => line.split(" ").map(Number));
function calculateTeamAbility(team) {
let sum = 0;
for (let i = 0; i < team.length; i++) {
for (let j = 0; j < team.length; j++) {
sum += ability[team[i]][team[j]];
}
}
return sum;
}
let min = Infinity;
function dfs(index, teamStart) {
if (teamStart.length === N / 2) {
const teamLink = [];
for (let i = 0; i < N; i++) {
if (!teamStart.includes(i)) {
teamLink.push(i);
}
}
const start = calculateTeamAbility(teamStart);
const link = calculateTeamAbility(teamLink);
min = Math.min(min, Math.abs(start - link));
return;
}
for (let i = index; i < N; i++) {
teamStart.push(i);
dfs(i + 1, teamStart);
teamStart.pop();
}
}
dfs(0, []);
console.log(min);
... 중략
이 과정을 반복하며 teamStart와 teamLink를 중복없이 구할 수 있다.

실행시간도 대폭 줄어든 모습을 볼 수 있다 !
넘넘 똑똑하시구...유식하시네요ㅎ
똑똑한남자가 이상형인데 제 이상형이랑 딱 맞으시네요~~^^
시간있으심 연락주시와요^^