#include <iostream>
using namespace std;
int N;
int map[21][21];
int ans = 100000000;
bool is_visited[21];
void DFS(int player, int cnt)
{
if (cnt == N / 2)
{
int score1 = 0;
int score2 = 0;
for (int i = 1; i <= N; i++)
{
for (int j = i + 1; j <= N; j++)
{
if (is_visited[i] && is_visited[j])
{
score1 += map[i][j];
score1 += map[j][i];
}
else if (!is_visited[i] && !is_visited[j])
{
score2 += map[i][j];
score2 += map[j][i];
}
}
}
ans = min(ans, abs(score1 - score2));
return;
}
for (int i = player + 1; i <= N; i++)
{
if (!is_visited[i])
{
is_visited[i] = true;
DFS(i, cnt + 1);
is_visited[i] = false;
}
}
}
int main()
{
cin >> N;
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
cin >> map[i][j];
}
}
DFS(0, 0);
cout << ans;
}
๋ ์์ง๋ ๋ฐฑํธ๋ํน์ด ๋๋ฌด ์ด๋ ต๋ค.......... DFS ์ํ์ง ๋๋ฌด ์ค๋๋ผ์ ์ฝ๋ ์ด์ผ ์ง๋์ง ๋ค ๊น๋จน์๋ค...
๋ ์ดํด ํ์ํจ..
๋์ถฉ ๋ชจ๋ ์ ์๋ค์ Backtracking์ผ๋ก visit์ 1/0 ๋ฐ๊ฟ ๊ฐ๋ฉด์ ๋ชจ๋ ๊ฒฝ์ฐ์ ์ ๋ณ๋ก score ์ฐจ์ด๋ฅผ ๊ตฌํ๊ณ , min ์ updateํด์ค
'๐ก๐ธ๐ธ๐ถ๐ฃ: ๐๐๐๐๐๐พ๐๐ฝ๐ > ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ3184 : ์ (Silver 1) (0) | 2023.02.05 |
---|---|
BOJ5671 : ํธํ ๋ฐฉ ๋ฒํธ (Silver 5) (0) | 2021.08.20 |
BOJ 2503 : ์ซ์์ผ๊ตฌ (Silver 5) (0) | 2021.08.20 |
BOJ17404 : RGB๊ฑฐ๋ฆฌ2 (Gold 4) (0) | 2021.08.17 |
BOJ1149 : RGB๊ฑฐ๋ฆฌ (Silver 1) (0) | 2021.08.17 |