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

27-9kyo-hwang #105

Merged
merged 3 commits into from
Feb 26, 2024
Merged

27-9kyo-hwang #105

merged 3 commits into from
Feb 26, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

@9kyo-hwang 9kyo-hwang commented Feb 14, 2024

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

1039 ๊ตํ™˜

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

3์ผ์— ๊ฑธ์ณ ์ด 3์‹œ๊ฐ„...?

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

1. ๋ฌธ์ œ

$1 \le N \le 1,000,000$์ด ์ฃผ์–ด์ง„๋‹ค.
์ฃผ์–ด์ง„ ์ˆ˜ N์˜ ์ž๋ฆฟ์ˆ˜๋ฅผ M์ด๋ผ ํ•  ๋•Œ, $1 \le i \lt j \le M$์ธ i, j๋ฅผ ์„ ํƒํ•ด i๋ฒˆ์งธ ์ˆซ์ž์™€ j๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๊ตํ™˜ํ•œ๋‹ค. ์ด ๋•Œ ๋ฐ”๊พผ ์ˆ˜๊ฐ€ 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.
์ˆซ์ž ๊ตํ™˜์„ $1 \le K \le 10$๋ฒˆ ์‹œํ–‰ํ–ˆ์„ ๋•Œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋ผ.

2. ์‹œํ–‰์ฐฉ์˜ค

ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‰ฌ์šด๋ฐ, ์ตœ๋Œ€ K๋ฒˆ ๊ตํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๋ฌด์กฐ๊ฑด K๋ฒˆ ๊ตํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ฃผ์–ด์ง€๋Š” 132 3์˜ ๊ฒฝ์šฐ, ๋ฐ˜๋“œ์‹œ 3๋ฒˆ ๊ตํ™˜ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€๊ฐ’์€ 321์ด ์•„๋‹Œ 312์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ฒ˜์Œ์—” "์–ด ์ด๊ฑฐ ์žฌ๊ท€๋กœ ํ•˜๋ฉด ์ฝ”๋“œ ๊น”๋”ํ•˜๊ฒŒ ๋‚˜์˜ค๊ฒ ๋Š”๋ฐ?" ํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์‹น์‹น ์ž‘์„ฑํ–ˆ๊ณ ,

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string N; int K;

string dfs(string n, int k = 0) {
    if(k == K) {
        return n;
    } else if(k > K) {
        return "-1";
    } else {
        string max_num = "-1";
        for(int i = 0; i < N.size() - 1; ++i) {
            for(int j = i + 1; j < N.size(); ++j) {
                string tmp = n;
                swap(tmp[i], tmp[j]);
                if(tmp[0] == '0') {
                    continue;
                }
                
                max_num = max(max_num, dfs(tmp, k + 1));
            }
        }
        return max_num;
    }
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    
    cin >> N >> K;
    cout << dfs(N);
    return 0;
}

์˜ˆ์ œ ํ…Œ์ผ€๋Š” ์ „๋ถ€ ๋งž๊ธธ๋ž˜ "์˜ค ๊ฐœ๊ฟ€๋ฌธ์ œ~" ์ด๋žฌ๋Š”๋ฐ
image
๊ท€์‹ ๊ฐ™์ด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋– ๋ฒ„๋ ธ๋‹ค.

๊ทธ๋ž˜์„œ ์Œ dfs๋กœ ํ•˜๋ฉด ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์ง€๋‚˜? bfs๋กœ ๋ฐ”๊ฟ”๋ด? ํ•ด์„œ bfs๋กœ ์ž‘์„ฑํ•ด๋ดค๋‹ค.

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    string N; int K;
    cin >> N >> K;
    
    deque<pair<string, int>> Q;
    Q.emplace_back(N, 0);
    
    string MaxNum = "-1";
    while(!Q.empty())
    {
        const auto [Num, SwapCnt] = Q.front();
        Q.pop_front();
        
        if(SwapCnt == K)
        {
            MaxNum = max(MaxNum, Num);
            continue;
        }
        
        for(int i = 0; i < N.size() - 1; ++i)
        {
            for(int j = i + 1; j < N.size(); ++j)
            {
                string TmpNum = Num;
                swap(TmpNum[i], TmpNum[j]);
                
                if(TmpNum[0] == '0')
                {
                    continue;
                }
                
                Q.emplace_back(TmpNum, SwapCnt + 1);
            }
        }
    }
    
    cout << MaxNum;
    return 0;
}

์ด๋ฒˆ์—๋„ ์˜ˆ์ œ๋Š” ๋‹ค ๋งž๊ธธ๋ž˜ ์Š ์ œ์ถœํ•ด๋ดค๋Š”๋ฐ
image
์ด๋ฒˆ์—” ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋œฌ๋‹ค(ใ…Žใ…Ž)

"์•„ ํ™•์ธํ–ˆ๋˜ ์ˆซ์ž๋“ค์€ ์ฒดํ‚นํ•ด์„œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๊ฒŒ ํ•ด๋ฒ„๋ ค?" ํ•ด์„œ Visited set์„ ์„ ์–ธํ•ด ์ด๋ฅผ ์‘์šฉํ•ด๋ดค์ง€๋งŒ...

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    string N; int K;
    cin >> N >> K;
    
    deque<pair<string, int>> Q;
    unordered_set<string> VisitedNum;
    
    Q.emplace_back(N, 0);

    string MaxNum = "-1";
    while(!Q.empty())
    {
        const auto [Num, SwapCnt] = Q.front();
        Q.pop_front();
        
        if(SwapCnt == K)
        {
            VisitedNum.emplace(Num);
            MaxNum = max(MaxNum, Num);
            continue;
        }
        
        for(int i = 0; i < N.size() - 1; ++i)
        {
            for(int j = i + 1; j < N.size(); ++j)
            {
                string TmpNum = Num;
                swap(TmpNum[i], TmpNum[j]);
                
                if(TmpNum[0] == '0' || VisitedNum.find(TmpNum) != VisitedNum.end())
                {
                    continue;
                }
                
                Q.emplace_back(TmpNum, SwapCnt + 1);
            }
        }
    }
    
    cout << MaxNum;
    return 0;
}

์—ฌ์ „ํžˆ ํ‹€๋ฆฐ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋œจ๋˜๊ฐ€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋œฌ๋‹ค.

์ด ๋•Œ๋ถ€ํ„ฐ ์ •์‹ ์ด ๋ฉํ•ด์กŒ๋‹ค. "์ด๊ฒŒ ๋ญ์ง€?"

์ผ๋ฐ˜์ ์ธ bfs์ฒ˜๋Ÿผ ์ƒˆ๋กœ์šด ์ˆ˜๋ฅผ ํ™•์ธํ•  ๋•Œ๋งˆ๋‹ค Visited๋ฅผ ๋„ฃ์–ด๋ฒ„๋ฆฌ๊ณ  ์‹ถ์ง€๋งŒ,

    while(!Q.empty())
    {
        const auto [Num, SwapCnt] = Q.front();
        Q.pop_front();
        
        cout << Num << ", " << SwapCnt << "\n";
        
        if(SwapCnt == K)
        {
            MaxNum = max(MaxNum, Num);
            continue;
        }
        
        for(int i = 0; i < N.size() - 1; ++i)
        {
            for(int j = i + 1; j < N.size(); ++j)
            {
                string TmpNum = Num;
                swap(TmpNum[i], TmpNum[j]);
                
                if(TmpNum[0] == '0' || VisitedNum.find(TmpNum) != VisitedNum.end())
                {
                    continue;
                }
                
                VisitedNum.emplace(TmpNum);
                Q.emplace_back(TmpNum, SwapCnt + 1);
            }
        }
    }

์ด ๊ฒฝ์šฐ 132 3์™€ ๊ฐ™์€ ์ผ€์ด์Šค์—์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค.

132, 0
312, 1
231, 1
123, 1
132, 2
213, 2
321, 2
-1

์ด๋Ÿฐ ์‹์œผ๋กœ 312๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด ์•ž์ „์— ๋ฐฉ๋ฌธํ–ˆ๋˜ ์ˆ˜๋ฅผ ๋‹ค์‹œ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•˜๋Š”๋ฐ, Visited ์ฒดํฌ ๋‹จ๊ณ„์—์„œ ๊ฑธ๋Ÿฌ์ง€๋ฉด์„œ K๋ฒˆ Swap ํ•˜๊ธฐ ์ „์— Queue์— ์›์†Œ๊ฐ€ ํ•˜๋‚˜๋„ ์กด์žฌํ•˜์ง€ ์•Š๊ฒŒ ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค.

"๋ญ์ง€ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•˜๋Š” ๊ฑฐ์ง€?" ํ•˜๊ณ  ๊ณ„์† ์ฝ”๋“œ๋ฅผ ๋น„ํ‹€์–ด๊ฐ€๋ฉฐ ํŠธ๋ผ์ดํ–ˆ์œผ๋‚˜...
image

์ด ์ฏค์—์„œ ์ •์‹ ์„ ์žƒ์–ด๋ฒ„๋ ธ๊ณ ... ๋ฉฐ์น ๋™์•ˆ ์ด ๋ฌธ์ œ๋ฅผ ๋ฌป์–ด๋’€์—ˆ๋‹ค....

3. ํ’€์ด

์˜ˆ์ „์— ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜์—…์‹œ๊ฐ„ ๋•Œ ํ’€์—ˆ๋˜ ๊ณผ์ œ๋ฅผ ๋ณด๋‹ค๊ฐ€ ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.
์ด ๊ณผ์ œ๋Š” ์ง์ง„ ๋˜๋Š” ์šฐํšŒ์ „๋งŒ ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋™์ฐจ๋ฅผ ํƒ€๊ณ  ํŠน์ • ์ขŒํ‘œ๊นŒ์ง€ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ง€๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฑฐ์˜€๋Š”๋ฐ, ์ด ๋•Œ ๋ฐฉ๋ฌธ ์ฒดํฌ๋ฅผ ๋‹จ์ˆœํžˆ [x][y] ์ขŒํ‘œ์— ๋Œ€ํ•ด์„œ ๊ฒ€์‚ฌํ•˜์ง€ ์•Š๊ณ  ํ•ด๋‹น ์นธ์— ์ง„์ž…ํ•  ๋•Œ์˜ ๋ฐฉํ–ฅ ์ •๋ณด๋„ ํ•จ๊ป˜ ๊ธฐ๋กํ–ˆ๋‹ค.
์ฆ‰ 3์ฐจ์› Visited ๋ฐฐ์—ด [d][x][y]๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์ธ๋ฐ, ์™œ๋ƒํ•˜๋ฉด ๊ฐ™์€ ์นธ์— ๋“ค์–ด์˜ค๋”๋ผ๋„ ์–ด๋Š ๋ฐฉํ–ฅ์—์„œ ๋“ค์–ด์™”๋Š” ์ง€์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ƒํ™ฉ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ ‡๋‹ค.

์ด๊ฑฐ๋ฅผ ๋ณด๊ณ  "์–ด? ๊ตํ™˜ ๋ฌธ์ œ๋„ ๋ช‡ ๋ฒˆ์งธ ๊ตํ™˜ํ–ˆ๋Š” ์ง€์— ๋”ฐ๋ผ ๊ตฌ๋ถ„ํ•ด์„œ Visit ์ฒดํฌ๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ํ•ด์ค˜์•ผ ํ•˜๋‚˜?" ์‹ถ์–ด์„œ ํ•œ ๋ฒˆ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ดค๋‹ค.

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

string N; int K;
vector<vector<bool>> Visited;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    cin >> N >> K;
    Visited.assign(K + 1, vector<bool>(1000001, false));
    
    cout << DFS(N, 0);
    
    return 0;
}

1039 ๊ตํ™˜-001
์ด๋Ÿฐ ์‹์œผ๋กœ ํ–‰์€ ์ˆซ์ž๋ฅผ k๋ฒˆ ๋ฐ”๊ฟจ์„ ๋•Œ๋ฅผ ์˜๋ฏธํ•˜๊ณ , ์—ด์€ 0๋ถ€ํ„ฐ 1,000,000๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Visited[2][132]๋Š” '์ˆซ์ž๋ฅผ 2๋ฒˆ ๋ฐ”๊ฟจ์„ ๋•Œ 132๋ฅผ ๋งŒ๋‚œ ์ ์ด ์žˆ๋Š”๊ฐ€'๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

int DFS(string Num, int Depth)
{
    if(Visited[Depth][stoi(Num)])
    {
        return -1;
    }
    else if(Depth == K)
    {
        return stoi(Num);
    }
    else
    {
        Visited[Depth][stoi(Num)] = true;
        
        int MaxNum = -1;
        for(int i = 0; i < N.size() - 1; ++i)
        {
            for(int j = i + 1; j < N.size(); ++j)
            {
                if(i == 0 && Num[j] == '0')
                {
                    continue;
                }
                
                string TmpNum = Num;
                swap(TmpNum[i], TmpNum[j]);
                
                MaxNum = max(MaxNum, DFS(TmpNum, Depth + 1));
            }
        }
        
        return MaxNum;
    }
}

ํ•˜๊ณ  ์ด Visited ๋ฐฐ์—ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ DFS ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค. ํ•ด๋‹น Depth์—์„œ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์žˆ๋Š” ์ˆซ์ž๋ผ๋ฉด ์ฆ‰์‹œ -1์„ returnํ•˜๋Š” ์กฐ๊ฑด์ด ์ถ”๊ฐ€๋๊ณ , recursive case์—์„œ๋Š” ํ•ด๋‹น Depth์—์„œ์˜ Num์„ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

image
ํ™€๋ฆฌ๋ชฐ๋ฆฌ๊ณผ์นด๋ชฐ๋ฆฌ... ๋“œ๋””์–ด ๋งž์ท„๋‹ค ํ‘ํ‘

4. ์ „์ฒด ์ฝ”๋“œ

  • 100๋งŒ์ด๋ž€ ์ˆซ์ž๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์„œ ์ด๋ฅผ set์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๋”๋‹ˆ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ค„์–ด๋“ค์—ˆ๋‹ค.
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_set>

using namespace std;

string N; int K;
unordered_set<int> Visited[11];

int DFS(string Num = N, int Depth = 0)
{
    if(Visited[Depth].find(stoi(Num)) != Visited[Depth].end())
    {
        return -1;
    }
    else if(Depth == K)
    {
        return stoi(Num);
    }
    else
    {
        Visited[Depth].emplace(stoi(Num));
        
        int MaxNum = -1;
        for(int i = 0; i < N.size() - 1; ++i)
        {
            for(int j = i + 1; j < N.size(); ++j)
            {
                if(i == 0 && Num[j] == '0')
                {
                    continue;
                }
                
                string TmpNum = Num;
                swap(TmpNum[i], TmpNum[j]);
                
                MaxNum = max(MaxNum, DFS(TmpNum, Depth + 1));
            }
        }
        
        return MaxNum;
    }
}

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    cin >> N >> K;

    cout << DFS();
    
    return 0;
}

image

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

๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ๋ณด๋‹ˆ๊นŒ ๊ฑฐ์˜ ๋‹ค BFS ์ฝ”๋“œ์ด๊ธธ๋ž˜, ๋‚˜๋„ BFS๋กœ ์ˆ˜์ •ํ•ด๋ณด์ž ์‹ถ์–ด์„œ ์ˆ˜์ •ํ–ˆ๋Š”๋ฐ ๊ณ„์† ํ‹€๋ฆฐ๋‹ค... ๋ญ์ง€?

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.

3์ผ๋™์•ˆ ์ •๋ง ์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค..๐Ÿ˜ฎ๐Ÿ‘
๋ฌธ์ œ ์ฝ์–ด๋ดค์„ ๋•Œ, ๋ฐฉํ–ฅ์ •๋ณด๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ „ํ˜€ ์ƒ๊ฐ์ง€๋„ ๋ชปํ–ˆ๋Š”๋ฐ ์ •๋ง ์‹ ๊ธฐํ•ฉ๋‹ˆ๋‹ค๐Ÿฅฒ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณผ์ œ์—์„œ ์ตํ˜”๋˜ ๊ธฐ์–ต์„ ๋ฐ”ํƒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ‘ธ์‹œ๋Š” ๊ฒƒ๋„ ๋ฉ‹์ง€์‹ญ๋‹ˆ๋‹ค!
์ €๋Š” ๊ณต๋ถ€ํ•ด๋„ ์ž˜ ๊นŒ๋จน๊ณ  ์‘์šฉํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ๋ณธ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค!๐Ÿ˜

Copy link
Collaborator

@Dolchae Dolchae left a comment

Choose a reason for hiding this comment

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

์ •๋ง ๋งค๋ฒˆ ๋Š๋ผ๋Š” ๊ฑฐ์ง€๋งŒ, ๊ฐ๋„ ์•ˆ ์žกํžˆ๋Š” ์ด๋Ÿฐ ๋ฌธ์ œ๋“ค์„ ์–ด๋–ป๊ฒŒ ํ’€์–ด๋‚ด์‹œ๋Š”๊ฑด์ง€.. ๊ฒƒ๋„ ์˜ค๋žœ์‹œ๊ฐ„๋™์•ˆ ํฌ๊ธฐํ•˜์ง€ ์•Š๊ณ ์š”..!๐Ÿ˜ฒ ํ•˜๋‚˜์˜ ์ˆซ์ž๋ผ๋„ ๊ตํ™˜ํšŸ์ˆ˜์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์œผ๋กœ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ๋ฐฉ์‹ ์ €๋ ‡๊ฒŒ ๋ฐ”๊พธ์‹  ๊ฒƒ๋„ ์ง„์งœ ๋Œ€๋‹จํ•˜์„ธ์š”,, ์ •๋ง ๊ณ ์ƒ๋งŽ์ดํ•˜์…จ์Šต๋‹ˆ๋‹ค!!๐Ÿ‘๐Ÿ˜Š

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.

์ด ๋ฌธ์ œ ์ž ์‹œ ๊ตฌ๊ฒฝ๋งŒ ํ•˜๊ณ , PR ๋ฐ”๋กœ ๋ณด๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ๋ญ”๊ฐ€ ๋ฌธ์ œ๊ฐ€ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์„œ ์ž ์‹œ๋งŒ ๋ณด๋ฅ˜ํ•ด๋’€๋‹ค๊ฐ€ ํ’€์–ด๋ณด๊ณ  ์‹ถ์–ด์š”...

@LJEDD2
Copy link

LJEDD2 commented Feb 25, 2024

์†Œ๋ฌธ์˜ ๊ทธ ๊ตฌ"๊ตํ™˜"๋ฌธ์ œ๊ตฐ์š”
๋‹ค์Œ์— ๊ผญ ํ•œ๋ฒˆ ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค

@gjsk132
Copy link
Member

gjsk132 commented Feb 25, 2024

์ด ๋ฌธ์ œ ์ง€๊ธˆ ์–ด์ œ๋ถ€ํ„ฐ ๊ฐ„๊ฐ„ํžˆ ๊ณ„์† ๋ณด๊ณ  ์žˆ๋Š”๋ฐ... ์ด์ „์˜ ์ œ๊ฐ€ ๋ญ”๊ฐ€ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ €๋ฅผ ๋งค์šฐ ์น˜๊ณ  ์‹ถ๋„ค์š”.

๊ทธ๋ž˜๋„ ํ•œ 3์‹œ๊ฐ„ ํˆฌ์žํ•ด์„œ ๋งž์ท„์Šต๋‹ˆ๋‹น! ๐Ÿ˜„

์‚ฌ์‹ค ์ €๋„ ์–ด๋–ป๊ฒŒ ํ’€์—ˆ๋Š”์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด์š”.

๋ฉ”๋ชจ๋ฆฌ ๊พธ์—ญ๊พธ์—ญ ์ค„์ด๊ณ ... ์‹œ๊ฐ„ ๊พธ์—ญ๊พธ์—ญ ์ค„์ด๊ณ ...

๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค ๊ธฐ์–ตํ•˜๋‹ˆ๊นŒ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋‚˜์„œ ๋ฐฐ์—ด 2๊ฐœ๋กœ ์Šค์œ„์นญํ•ด๋ฒ„๋ฆฌ๊ณ 

๊ฐ™์€ ์ˆซ์ž ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ ๊ณ ๋ ค ์•ˆํ•˜๋‹ˆ๊นŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋‚˜์„œ set์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋‹ˆ๊นŒ ์–ด์ฐŒ์ €์ฐŒ... ํ•ด๊ฒฐ๋์Šต๋‹ˆ๋‹ค.

๋ฐฑํŠธ๋ž˜ํ‚น ์ฝ”๋“œ
input = open(0).readline

num, cnt = map(int,input().split())

memo = [set(), set()]

memo[0].add(num)

for i in range(cnt):
    while memo[i%2]:
        
        n = list(str(memo[i%2].pop()))

        for p1 in range(len(n)):
            for p2 in range(p1):
                n[p1], n[p2] = n[p2], n[p1]
                memo[(i+1)%2].add(int("".join(n)))
                n[p1], n[p2] = n[p2], n[p1]

if memo[cnt%2]:
    answer = max(memo[cnt%2])
    print(answer if len(str(answer)) == len(str(num)) else -1)
else:
    print(-1)

๋‹ค์Œ์—” ์ฝ”๋ฉ˜ํŠธ์— ๊ธฐ๋‹ค๋ ค๋‹ฌ๋ผํ•˜์ง€ ๋ง๊ณ  merge ํ•ด๋‹ฌ๋ผ๊ณ  ํ•ด์•ผ๊ฒ ๋‹ค...

๋ฆฌ๋ทฐ ๊ธฐ๋‹ค๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ด์—ฌ ๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘

@tgyuuAn
Copy link
Member

tgyuuAn commented Feb 25, 2024

์ด ๋ฌธ์ œ ์ง€๊ธˆ ์–ด์ œ๋ถ€ํ„ฐ ๊ฐ„๊ฐ„ํžˆ ๊ณ„์† ๋ณด๊ณ  ์žˆ๋Š”๋ฐ... ์ด์ „์˜ ์ œ๊ฐ€ ๋ญ”๊ฐ€ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ €๋ฅผ ๋งค์šฐ ์น˜๊ณ  ์‹ถ๋„ค์š”.

๊ทธ๋ž˜๋„ ํ•œ 3์‹œ๊ฐ„ ํˆฌ์žํ•ด์„œ ๋งž์ท„์Šต๋‹ˆ๋‹น! ๐Ÿ˜„

์‚ฌ์‹ค ์ €๋„ ์–ด๋–ป๊ฒŒ ํ’€์—ˆ๋Š”์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด์š”.

๋ฉ”๋ชจ๋ฆฌ ๊พธ์—ญ๊พธ์—ญ ์ค„์ด๊ณ ... ์‹œ๊ฐ„ ๊พธ์—ญ๊พธ์—ญ ์ค„์ด๊ณ ...

๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค ๊ธฐ์–ตํ•˜๋‹ˆ๊นŒ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋‚˜์„œ ๋ฐฐ์—ด 2๊ฐœ๋กœ ์Šค์œ„์นญํ•ด๋ฒ„๋ฆฌ๊ณ 

๊ฐ™์€ ์ˆซ์ž ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ ๊ณ ๋ ค ์•ˆํ•˜๋‹ˆ๊นŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋‚˜์„œ set์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋‹ˆ๊นŒ ์–ด์ฐŒ์ €์ฐŒ... ํ•ด๊ฒฐ๋์Šต๋‹ˆ๋‹ค.

๋ฐฑํŠธ๋ž˜ํ‚น ์ฝ”๋“œ
๋‹ค์Œ์—” ์ฝ”๋ฉ˜ํŠธ์— ๊ธฐ๋‹ค๋ ค๋‹ฌ๋ผํ•˜์ง€ ๋ง๊ณ  merge ํ•ด๋‹ฌ๋ผ๊ณ  ํ•ด์•ผ๊ฒ ๋‹ค...

๋ฆฌ๋ทฐ ๊ธฐ๋‹ค๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ด์—ฌ ๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘

"์ด์ „์˜ ์ œ๊ฐ€ ๋ญ”๊ฐ€ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ €๋ฅผ ๋งค์šฐ ์น˜๊ณ  ์‹ถ๋„ค์š”."

...

๐Ÿ˜๐Ÿ˜๐Ÿ˜




@9kyo-hwang 9kyo-hwang merged commit f2e30dd into main Feb 26, 2024
@9kyo-hwang 9kyo-hwang mentioned this pull request Feb 29, 2024
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.

6 participants