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

44-9kyo-hwang #164

Merged
merged 4 commits into from
May 23, 2024
Merged

44-9kyo-hwang #164

merged 4 commits into from
May 23, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

@9kyo-hwang 9kyo-hwang commented May 6, 2024

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

6987 ์›”๋“œ์ปต

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

1์‹œ๊ฐ„ + ๋ชป์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ

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

ํ•˜ ์™œ ์ด๊ฒŒ ์•ˆ๋– ์˜ฌ๋ž์„๊นŒ...

1. ๋ฌธ์ œ

6๊ฐœ๊ตญ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฐ ์กฐ๋ณ„๋กœ ๋™์ผํ•œ ์กฐ์— ์†Œ์†๋œ ๊ตญ๊ฐ€๋“ค๊ณผ ํ•œ ๋ฒˆ์”ฉ, ๊ฐ ๊ตญ๊ฐ€๋ณ„๋กœ ์ด 5๋ฒˆ์˜ ๊ฒฝ๊ธฐ๋ฅผ ์น˜๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ๋‚˜๋ผ์˜ ์Šน, ๋ฌด์Šน๋ถ€, ํŒจ์˜ ์ˆ˜๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ์ธ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋ผ.

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜ 2๊ฐœ์˜ ๊ฒฐ๊ณผ๋Š” ๊ฐ€๋Šฅํ•˜๋‚˜

๋‚˜๋ผ ์Šน ๋ฌด ํŒจ
A 5 0 0
B 3 0 2
C 2 0 3
D 0 0 5
E 4 0 1
F 1 0 4
๋‚˜๋ผ ์Šน ๋ฌด ํŒจ
A 4 1 0
B 3 0 2
C 4 1 0
D 1 1 3
E 0 0 5
F 1 1 3

์•„๋ž˜ 2๊ฐœ์˜ ๊ฒฐ๊ณผ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

๋‚˜๋ผ ์Šน ๋ฌด ํŒจ
A 5 0 0
B 4 0 1
C 2 2 1
D 2 0 3
E 1 0 4
F 0 0 5
๋‚˜๋ผ ์Šน ๋ฌด ํŒจ
A 5 0 0
B 3 1 1
C 2 1 1
D 2 0 3
E 0 0 5
F 1 0 4

2. ํ’€์ด

A, B, C, D, E, F 6๊ฐœ์˜ ํŒ€์ด ๊ฐ ํŒ€๋“ค๊ณผ ํ•œ ๋ฒˆ์”ฉ ์น˜๋ฅด๋Š” ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด 15๊ฐ€์ง€๋‹ค.

  • A, B
  • A, C
  • A, D
  • A, E
  • A, F
  • B, C
  • B, D
  • B, E
  • B, F
  • C, D
  • C, E
  • C, F
  • D, E
  • D, F
  • E, F

๊ฐ ๋งค์น˜๋งˆ๋‹ค ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด 3๊ฐ€์ง€๋‹ค

  • ํŒ€ 1 ์Šน๋ฆฌ
  • ํŒ€ 2 ์Šน๋ฆฌ
  • ๋ฌด์Šน๋ถ€

๋”ฐ๋ผ์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‹ค ๋Œ์•„๋ณด๋ฉด $3^{15} = 14,348,907$ ๋ฒˆ์˜ ์—ฐ์‚ฐ์ด ๋ฐœ์ƒํ•œ๋‹ค. ์ถฉ๋ถ„ํžˆ ์‹œ๊ฐ„ ์•ˆ์— ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ "๋ฐฑํŠธ๋ž˜ํ‚น"์„ ์ด์šฉํ•ด ํ’€์–ด๋ณธ๋‹ค.

for _ in range(4):
    scores = list(map(int, input().split()))
    scores = [scores[i:i+3] for i in range(0, 18, 3)]
    print(1 if backtracking() else 0, end=' ')

4๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ๊ฒฐ๊ณผ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด (1 x 18) ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง€๋ฏ€๋กœ (6 x 3)์œผ๋กœ ์žฌ๋ฐฐ์น˜ํ•œ๋‹ค.
์ดํ›„ backtracking ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด True๋ฉด 1, False๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. True๋ฉด ์ฃผ์–ด์ง„ scores๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๋ž€ ๋œป์ด๊ณ , False๋ฉด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๋ž€ ๋œป์ด๋‹ค.

matches = list(combinations(range(6), 2))

def backtracking(game: int = 0) -> bool:
    if game == len(matches):
        for score in scores:
            if score.count(0) != 3:
                return False
                
        return True
    ...

backtracking ํ•จ์ˆ˜์—์„œ base case์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค.
game ๋ณ€์ˆ˜๋Š” 15๊ฐœ์˜ ๋งค์น˜์—์„œ ๋ช‡ ๋ฒˆ์งธ ๋งค์น˜์ธ ์ง€ ๋‚˜ํƒ€๋‚ธ๋‹ค. matches์—๋Š” ์œ„์—์„œ ๋งํ•œ (A,B), (A, C), ...๊ฐ€ ์ˆซ์ž๋กœ ํ‘œํ˜„๋œ (0, 1), (0, 2), ... ์ด๋Ÿฐ ๊ฐ’๋“ค์ด ์ €์žฅ๋ผ์žˆ๋‹ค.
์ฆ‰ game ๋ณ€์ˆ˜ ๊ฐ’์ด 15์— ๋„๋‹ฌํ•˜๋ฉด ๋ชจ๋“  ๊ฒฝ๊ธฐ๋ฅผ ๋‹ค ์ˆ˜ํ–‰ํ•ด๋ดค๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ scores ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•ด๋ณธ๋‹ค.
์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์•Œ๊ฒ ์ง€๋งŒ, [1ํŒ€ ์Šน - 2ํŒ€ ํŒจ], [1ํŒ€ ๋ฌด - 2ํŒ€ ๋ฌด], [1ํŒ€ ํŒจ - 2ํŒ€ ์Šน] 3๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ํ™•์ธํ•ด๋ณด๊ธฐ ์œ„ํ•ด scores ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋œ ๊ฐ’์„ "1์”ฉ ์ค„์—ฌ๋ณธ๋‹ค"

์˜ˆ๋ฅผ ๋“ค์–ด

๋‚˜๋ผ ์Šน ๋ฌด ํŒจ
A 5 0 0
B 3 0 2

์ด ๋‘ ํŒ€์— ๋Œ€ํ•ด์„œ ๊ฒฝ์šฐ๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค ํ•  ๋•Œ

  1. A ์Šน, B ํŒจ -> Scores[A][Win] -= 1, Scores[B][Lose] -= 1
  2. A ๋ฌด, B ๋ฌด -> Scores[A][Tie] -= 1, Scores[B][Tie] -= 1
  3. A ํŒจ, B ์Šน -> Scores[A][Lose] -= 1, Scores[B][Win] -= 1

์ด๋Ÿฐ ์‹์œผ๋กœ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

15๊ฐ€์ง€ ๋งค์น˜๋ฅผ ๋ชจ๋‘ ์™„๋ฃŒํ•˜๋ฉด Score ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๋Š”๋ฐ, ๋งŒ์•ฝ ๋ชจ๋“  ํŒ€์˜ ์Šค์ฝ”์–ด๊ฐ€ "0 0 0"์œผ๋กœ ๋˜์–ด์žˆ๋‹ค๋ฉด ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ๋Š” ์Šค์ฝ”์–ด ์•ฝ 1400๋งŒ ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜์˜ ์ผ€์ด์Šค์— ๋ถ€ํ•ฉํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

result_indices = ((0, 2), (1, 1), (2, 0))

t1, t2 = matches[game]

for i, j in result_indices:
    if scores[t1][i] <= 0 or scores[t2][j] <= 0:
        continue
    
    scores[t1][i] -= 1
    scores[t2][j] -= 1
    
    if backtracking(game + 1):
        return True
    
    scores[t1][i] += 1
    scores[t2][j] += 1

return False

backtracking ํ•จ์ˆ˜์˜ recursive case์ด๋‹ค. ํ˜„์žฌ ๋งค์น˜(game)์— ํ•ด๋‹นํ•˜๋Š” ํŒ€ 1, 2๋ฅผ matches์—์„œ ๊บผ๋‚ธ๋‹ค.
๊ทธ๋ฆฌ๊ณ  result_indices์—์„œ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•œ๋‹ค. ๊ฐ ์ˆซ์ž 0, 1, 2๋Š” ๋‹ค์Œ์„ ์˜๋ฏธํ•œ๋‹ค.

  • scores[team][0]: ์ด๊ธด ํšŸ์ˆ˜
  • scores[team][1]: ๋น„๊ธด ํšŸ์ˆ˜
  • scores[team][2]: ์ง„ ํšŸ์ˆ˜

์ฆ‰

  • (0, 2): ํŒ€ t1์€ ์ด๊ธฐ๊ณ , ํŒ€ t2๋Š” ์ง
  • (1, 1): ํŒ€ t1, t2๊ฐ€ ๋น„๊น€
  • (2, 0): ํŒ€ t1์€ ์ง€๊ณ , ํŒ€ t2๋Š” ์ด๊น€

๋“ค์–ด์žˆ๋Š” ์ˆ˜๋ฅผ 1์”ฉ ๊นŽ์œผ๋ฉด์„œ ๊ฐ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•ด๋ณด๊ณ , backtrackingํ•œ ๊ฒฐ๊ณผ๊ฐ€ True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์‹์ด๋‹ค.
๋งŒ์•ฝ False๋ผ๋ฉด ํ•ด๋‹น ๊ฒฝ์šฐ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ๋‹ค์‹œ ์›๋ณต์‹œํ‚จ ๋’ค ๋‹ค์Œ ๊ฒฝ์šฐ๋ฅผ ํ™•์ธํ•œ๋‹ค.
์ด ๋•Œ ์–ด๋Š ํ•œ ํŒ€์˜ ์ ์ˆ˜๊ฐ€ ์ด๋ฏธ 0 ์ดํ•˜๋ผ๋ฉด ํ•ด๋‹น ๊ฒฝ์šฐ๋Š” ํ™•์ธํ•˜์ง€ ์•Š๋Š”๋‹ค. 0 ๋ฐ‘์œผ๋กœ ๋‚ด๋ ค๊ฐ€๋Š” ๊ฑด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ €๋ ‡๊ฒŒ result_indices๋ฅผ ๋ชจ๋‘ ํ™•์ธํ–ˆ๋Š”๋ฐ๋„ ์ค‘๊ฐ„์— True๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ์• ์ดˆ์— ํ˜„์žฌ ์ƒํƒœ ์ž์ฒด๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ False๋กœ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค.

์ „์ฒด ์ฝ”๋“œ

from itertools import combinations
input = open(0).readline

matches = list(combinations(range(6), 2))
result_indices = ((0, 2), (1, 1), (2, 0))

def backtracking(game: int = 0) -> bool:
    if game == len(matches):
        for score in scores:
            if score.count(0) != 3:
                return False
                
        return True
    
    t1, t2 = matches[game]
    
    for i, j in result_indices:
        if scores[t1][i] <= 0 or scores[t2][j] <= 0:
            continue
        
        scores[t1][i] -= 1
        scores[t2][j] -= 1
        
        if backtracking(game + 1):
            return True
        
        scores[t1][i] += 1
        scores[t2][j] += 1
    
    return False

for _ in range(4):
    scores = list(map(int, input().split()))
    scores = [scores[i:i+3] for i in range(0, 18, 3)]
    print(1 if backtracking() else 0, end=' ')
  • C++
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const vector<pair<int, int>> TeamMatches
{
    {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, 
    {1, 2}, {1, 3}, {1, 4}, {1, 5},
    {2, 3}, {2, 4}, {2, 5},
    {3, 4}, {3, 5},
    {4, 5},
};

const vector<pair<int, int>> MatchResultIndices
{// 1 win,   tie,   2 win
    {0, 2}, {1, 1}, {2, 0}
};

vector<vector<int>> MatchScore;

bool Backtracking(int Match = 0)
{
    if(Match == TeamMatches.size())
    {
        for(const auto& Team : MatchScore)
        {
            if(count(Team.begin(), Team.end(), 0) != 3)
            {
                return false;
            }
        }
        
        return true;
    }
    
    const auto& [Team1, Team2] = TeamMatches[Match];
    for(const auto& [i, j] : MatchResultIndices)
    {
        if(MatchScore[Team1][i] <= 0 || MatchScore[Team2][j] <= 0)
        {
            continue;
        }
        
        --MatchScore[Team1][i];
        --MatchScore[Team2][j];
        
        if(Backtracking(Match + 1))
        {
            return true;
        }
        
        ++MatchScore[Team1][i];
        ++MatchScore[Team2][j];
    }
    
    return false;
}

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    for(int T = 1; T <= 4; ++T)
    {
        MatchScore.assign(6, vector(3, 0));
        for(auto& Team : MatchScore)
        {
            for(auto& Score : Team)
            {
                cin >> Score;
            }
        }
        cout << Backtracking() << " ";
    }

    return 0;
}

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

ํ—ˆ ์™œ ์ด๊ฒŒ ์•ˆ๋ณด์˜€์„๊นŒ... ์ฃผ์–ด์ง„ ํŒ€ ๊ฐœ์ˆ˜๊ฐ€ ๊ทน๋„๋กœ ์ ์–ด์„œ ์™„ํƒ๋•Œ๋ฆฌ๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค๋ž€ ์ƒ๊ฐ์ด ๋“ค๊ธด ํ–ˆ๋Š”๋ฐ "์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ์ง€" ๊ฐ์ด ์•ˆ์žกํ˜€์„œ ์‚ฝ์งˆ ๊ฑฐํ•˜๊ฒŒ ํ–ˆ๋‹ค.
ํฐ์ผ๋‚ฌ๋„ค...

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

๋ฆฌ๋ทฐ ๋Šฆ์–ด์„œ ๋งค์šฐ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค..๐ŸงŽ๐Ÿปโ€โ™€๏ธ
ํ•œ ์ผ์ฃผ์ผ ์ „์— ๋„์ „ํ•ด๋ดค๋‹ค๊ฐ€ ์•ˆ๋˜์–ด์„œ ์˜ค๋Š˜ ๋‹ค์‹œ ๋„์ „ํ•ด๋ดค๋Š”๋ฐ ์—ญ์‹œ ํ‹€๋ฆฌ๋Š”๊ตฐ์š”..๐Ÿคฏ
PR๊ณผ ๋ธ”๋กœ๊ทธ ์ฐธ๊ณ ํ•ด์„œ ํ’€์–ด๋ด๋„ ํ…Œ์ผ€๋Š” ํ†ต๊ณผํ•˜์ง€๋งŒ ํ‹€๋ฆฌ๋„ค์š”?? ํ—ˆํ—ˆ..
์ €๋Š” ์ผ์ผ์ด (0, 2), (1, 1), (2, 0)์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ฝ”๋“œ๋ฅผ ์งฐ๋Š”๋ฐ ํ™•์‹คํžˆ result_indices = ((0, 2), (1, 1), (2, 0)) ์ด๋ ‡๊ฒŒ ๋ฌถ์–ด์„œ for๋ฌธ์œผ๋กœ ํ‘ธ๋‹ˆ ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.๐Ÿ‘๐Ÿ‘

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

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

์—ฌ์ฐจ์ €์ฐจ ๋ฐฑํŠธ๋ ˆํ‚น๊นŒ์ง€๋Š” ์ƒ๊ฐํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ, ์ฝ”๋“œ ์งœ๋Š” ๊ณผ์ •์—์„œ ํฌ๊ธฐํ•ฉ๋‹ˆ๋‹ค.

4๋ฒˆ์˜ ๊ฒฝ๊ธฐ ๊ฐ’์„ ๊ธฐ์–ตํ•ด๋‘๊ณ , ๊ฐ ๊ฒฝ๊ธฐ๋“ค์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•ด์„œ, ๊ทธ๊ฒŒ ์ฃผ์–ด์ง„ ๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ์™€ ๊ฐ™์€ ๊ฐ’์ด ์žˆ์œผ๋ฉด True๋กœ ๋ฐ”๊พธ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ...

Recursive ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š”๊ฒŒ ์•„์ง ์—ฐ์Šต์ด ๋งŽ์ด ํ•„์š”ํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๐Ÿฅฒ

( ๋ญ”๊ฐ€ ๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ๋งŒ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•˜๋Š”๋ฐ... ์•ˆ๋˜๋„ค์š” )

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