๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐“ก๐“ธ๐“ธ๐“ถ๐Ÿฃ: ๐’œ๐“๐‘”๐‘œ๐“‡๐’พ๐“‰๐’ฝ๐“‚/์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด

BOJ14889 : ์Šคํƒ€ํŠธ์™€ ๋งํฌ (Silver 3)

#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ํ•ด์คŒ