Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13-g0rnn #52

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

13-g0rnn #52

wants to merge 3 commits into from

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Jan 5, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์Šคํƒ€ํŠธ์™€ ๋งํฌ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

45m

โ— ๋ฌธ์ œ๊ฐ€ ์–ด๋ ต๋‹ค๋ฉด ํ˜น์€ ์ดํ•ด๊ฐ€ ์•ˆ๋˜๋ฉด ํ˜น์€ ์• ๋งคํ•˜๋‹ค๋ฉด

๋ฐฑํŠธ๋ ˆํ‚น์—์„œ N๊ณผ M 1๋ฒˆ๊ณผ 2๋ฒˆ์„ ํ’€์–ด๋ณด์‹ฌ์ด ์ข‹์„ ๋“ฏํ•ฉ๋‹ˆ๋‹ค.

์œ„ ๋ฌธ์ œ๊ฐ€ ์™œ ๋ฐฑํŠธ๋ ˆํ‚น์— ํ•ด๋‹นํ•˜๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค๋ฉด ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ์„ค๋ช…ํ•ด์ฃผ๋Š” ์‚ฌ์ดํŠธ์— ๋“ค์–ด๊ฐ€์„œ ํ’€์ด์™€ ํ•จ๊ป˜ ๊ฐœ๋…์„ ์žก์•„๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

๊ฐœ๋…์„ ์ž˜ ์žก์•˜๋‹ค๋ฉด 1๋ฒˆ์„ ํ’€๊ณ  2๋ฒˆ ํ’€ ๋•Œ์— 3์ดˆ ์•ˆ์— ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹น ^^

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์กฐํ•ฉ(combination)์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ ๊ฐ™์•„ ๋ณด์ž…๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜๋Š” ๋ฐฑํŠธ๋ ˆํ‚น์ด์ง€๋งŒ ์ €๋Š” ์กฐํ•ฉ์„ ๊ตฌํ˜„ํ•ด๋‚ด๋Š” ๊ฒŒ ํ•ต์‹ฌ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ „์— ์ œ๊ฐ€ ์˜ฌ๋ฆฐ pr์—์„œ dfs๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ๋„ ๊ทธ๊ฒƒ์ฒ˜๋Ÿผ dfs๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๊ณ  ์ž‘์—…์ด ๋๋‚˜๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ ์ˆ˜์ •ํ•œ ๋‚ด์—ญ์„ ๋ณต๊ตฌํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€๋ฆฝ๋‹ˆ๋‹ค.

๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š” ์•„์ด๋””์–ด์™€ ์ˆ˜๋„ ์ฝ”๋“œ๋ฅผ ์†Œ๊ฐœํ•˜๋ฉฐ ์ œ ํ’€์ด๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

[idea]
1. ๋ฐฐ์—ด index๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ๋žŒ์„ ๊ตฌ๋ณ„ํ•˜๊ณ  ๊ฐ ์—”ํŠธ๋ฆฌ์—๋Š” ๋ฐฉ๋ฌธ ์—ฌ๋ถ€(ํ˜น์€ ์„ ํƒ ์—ฌ๋ถ€)๋ฅผ ์ €์žฅํ•œ๋‹ค.
2. n๋ช…์˜ ์‚ฌ๋žŒ ์ค‘ ์ ˆ๋ฐ˜์„ ์„ ํƒํ•˜๋ฉด ํŒ€์ด ๋‚˜๋ˆ ์ง„ ๊ฒƒ์ด๋‹ค.

[pseudo code]
1. dfs๋ฅผ ํ†ตํ•ด n/2๋ช…์˜ ์‚ฌ๋žŒ์„ ๋ฝ‘๋Š”๋‹ค.
  - index๋ฅผ ์ค‘๋ณตํ•˜์—ฌ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜
  - ์‚ฌ๋žŒ์„ ๋ฝ‘๊ณ  ์ž‘์—…์„ ๋งˆ์น˜๋ฉด ์›์ƒ๋ณต๊ท€
  - n/2 ๋ช…์„ ๋ฝ‘์€ ํ›„์—” ํฌ์ธํŠธ ๊ณ„์‚ฐ
2. ํฌ์ธํŠธ๋Š” visited๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ true์ธ๊ฒƒ๊ณผ false์ธ ๊ฒƒ์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ํ•ฉํ•œ๋‹ค.
3. ์ตœ์†Œ ์ ์ˆ˜ ์ฐจ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.

์ฝ”๋“œ๋ฅผ ๋ณด์•˜์„ ๋•Œ dfs ํ•จ์ˆ˜์™€ sumPoint ํ•จ์ˆ˜์˜ ๋™์ž‘ ๋ฐฉ์‹์ด ๊ฐ™๋‹ค๋Š” ๊ฒƒ์„ ๋ˆˆ์น˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. dfs๋ฅผ ํ•ด์ฒดํ•ด๋ณด๋ฉด for๋ฌธ์ด ์•„๋งˆ n๊ฐœ ์ค‘์ฒฉ๋˜์–ด ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ for๋ฌธ์€ ์ด์ „ for๋ฌธ์˜ index ๋’ค์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

clion์—์„œ๋Š” #include <vector>๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค..

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์œ ํŠœ๋ธŒ ๋ณด๋ฉด์„œ ํ•ด์„œ 1์‹œ๊ฐ„์ด๋‚˜ ๋„˜๊ฒŒ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค...
๊ทธ๋ฆฌ๊ณ  ์˜ˆ์ „์— ์‹œํ—˜ ๋ฌธ์ œ๋กœ ๋งˆ์ฃผํ–ˆ๋˜ ์กฐํ•ฉ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๋ณด๋‹ˆ ๊ผญ ํ˜ผ์ž ํž˜์œผ๋กœ ํ’€๊ณ ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.
(๊ทธ๋ฆฌ๊ณ  ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž)

2๊ฐœ๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๊ณ , ์ง€๊ธˆ๊นŒ์ง€ ์ž์ฃผ ์จ์™”๋˜ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

int dfs() {
    if (base case) return ~ ; 

    flag[k] = true;
    int a = dfs(flag, k + 1, count + 1);
    flag[k] = false;
    int b = dfs(flag, k + 1, count);

    return ~ ;
}

์ด๋Ÿฐ ์‹์œผ๋กœ bool๋ฐฐ์—ด์— true ์™€ false๋ฅผ ๋„ฃ์œผ๋ฉฐ basecase์— ๋„๋‹ฌํ–ˆ์„๋–„ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋ฉด ๋ฌธ์ œ์— ๋งž๋Š” ๋‹ต์„ ๊ตฌํ•˜๊ณ  return ํ•ฉ๋‹ˆ๋‹ค. (์—ฌ๊ธฐ์„œ๋Š” Ability์˜ Diff)
๊ทธ ๋‹ค์Œ ๋ฐฑํŠธ๋ž˜ํ‚นํ•˜๋ฉฐ ์ตœ์†Œ diff๋ฅผ ์ฐพ๋Š” ๋กœ์ง์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ 
์ œ๊ฐ€ ๊ตฌํ˜„ํ•œ ํŒ€์›์˜ ๋Šฅ๋ ฅ์น˜๋ฅผ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ๋Š” ์•Œ์•„๋ณด๊ธฐ๋Š” ์‰ฝ์ง€๋งŒ ์กฐ๊ธˆ ๊ธด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
๊ท ํ˜ธ๋‹˜์€ visited์™€ s์˜ ์ธ๋ฑ์Šค๋ฅผ ์ž˜ ์กฐํ•ฉํ•ด์„œ ํ•œ ๋ฒˆ์— ๋”ํ•˜์…จ๋„ค์š”. ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

CPP CODE
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

using namespace std;

int N;
vector<vector<int>> team;

int getAbilityDiff(vector<bool>& flag) {
    vector<int> myTeam, yourTeam;
    for (int i = 1; i <= N; i++) {
        if (flag[i]) myTeam.push_back(i);
        else yourTeam.push_back(i);
    }

    int my = 0, your = 0;
    for (int y : myTeam) {
        for (int x : myTeam) 
            my += team[y][x];
    }
    for (int y : yourTeam) {
        for (int x : yourTeam) 
            your += team[y][x];
    }
    
    return abs(my - your);
}

int dfs(vector<bool>& flag, int k, int count) {
    if (count > N / 2) return INT_MAX;
    if (k == N) {
        if (count == N / 2) return getAbilityDiff(flag);
        return INT_MAX;
    }
    flag[k] = true;
    int select = dfs(flag, k + 1, count + 1);
    flag[k] = false;
    int notSelect = dfs(flag, k + 1, count);

    return min(select, notSelect);
}

int main() {
    cin >> N;

    team = vector<vector<int>>(N + 1, vector<int>(N + 1, 0));
    for (int y = 1; y <= N; y++)
        for (int x = 1; x <= N; x++) 
            cin >> team[y][x];

    vector<bool> flag(N + 1, false);
    cout << dfs(flag, 1, 0);

    return 0;
}

@kokeunho
Copy link
Collaborator

์กฐํ•ฉ ๋ฌธ์ œ๋Š” ์ฒ˜์Œ์ธ๋ฐ
ํ™•์‹คํžˆ ์ธ์› ์ˆ˜๋ฅผ ๋‚˜๋ˆˆ ์กฐํ•ฉ์„ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ์‰ฝ์‚ฌ๋ฆฌ ๋– ์˜ค๋ฅด์ง€ ์•Š๋”๋ผ๊ตฌ์š”.
๊ทธ๋ž˜์„œ ๊ท ํ˜ธ๋‹˜์˜ ํ’€์ด๋ฅผ ์ฐธ์กฐํ–ˆ๊ณ  ๋•๋ถ„์— ๋ฐฑํŠธ๋ž˜ํ‚น์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
์ฝ”๋ฉ˜ํŠธ ๋‚ด์šฉ๋„ ๋Š˜๋ฆด ๊ฒธ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ ์งค๋ง‰ํ•˜๊ฒŒ ๋‚จ๊ฒจ๋ด…๋‹ˆ๋‹ค.
๋ฐฑํŠธ๋ž˜ํ‚น
๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํ•ด๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉ(dfs ๊ธฐ๋ฐ˜)
์žฅ์ : ์กฐ๊ฑด์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ๋กœ์—์„œ๋Š” ๋˜๋Œ์•„ ๊ฐ€(backTracking) ๋ถˆํ•„์š”ํ•œ ํƒ์ƒ‰์„ ์ค„์ž„
๊ตฌ์กฐ: 1) ์ข…๋ฃŒ ์กฐ๊ฑด ํ™•์ธ
2) ๊ฐ€๋Šฅ์„ฑ ํŒ๋‹จ(๊ฐ€์ง€์น˜๊ธฐ)
3) ์žฌ๊ท€ ํ˜ธ์ถœ
4) ๋ฐฑํŠธ๋ž˜ํ‚น (ํƒ์ƒ‰ ์™„๋ฃŒ ํ›„ ์›๋ž˜ ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ)

์ด๋ฒˆ PR ๋„ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants