๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐˜š๐˜ญ๐˜ฐ๐˜ธ ๐˜ฃ๐˜ถ๐˜ต ๐˜ด๐˜ต๐˜ฆ๐˜ข๐˜ฅ๐˜บ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์—ฌํ–‰๊ฒฝ๋กœ - C++ ๋ณธ๋ฌธ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ค€๋น„/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์—ฌํ–‰๊ฒฝ๋กœ - C++

.23 2024. 11. 8. 11:20
๋ฌธ์ œ

์ฃผ์–ด์ง„ ํ•ญ๊ณต๊ถŒ์„ ๋ชจ๋‘ ์ด์šฉํ•˜์—ฌ ์—ฌํ–‰๊ฒฝ๋กœ๋ฅผ ์งœ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•ญ์ƒ "ICN" ๊ณตํ•ญ์—์„œ ์ถœ๋ฐœํ•ฉ๋‹ˆ๋‹ค.

ํ•ญ๊ณต๊ถŒ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด tickets๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋ฐฉ๋ฌธํ•˜๋Š” ๊ณตํ•ญ ๊ฒฝ๋กœ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ
  • ๋ชจ๋“  ๊ณตํ•ญ์€ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž 3๊ธ€์ž๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
  • ์ฃผ์–ด์ง„ ๊ณตํ•ญ ์ˆ˜๋Š” 3๊ฐœ ์ด์ƒ 10,000๊ฐœ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • tickets์˜ ๊ฐ ํ–‰ [a, b]๋Š” a ๊ณตํ•ญ์—์„œ b ๊ณตํ•ญ์œผ๋กœ ๊ฐ€๋Š” ํ•ญ๊ณต๊ถŒ์ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
  • ์ฃผ์–ด์ง„ ํ•ญ๊ณต๊ถŒ์€ ๋ชจ๋‘ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋งŒ์ผ ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๊ฐ€ 2๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๊ฐ€ ์•ž์„œ๋Š” ๊ฒฝ๋กœ๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ๋„์‹œ๋ฅผ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ
tickets return
[["ICN", "JFK"], ["HND, "IAD"], ["JFK", "HND"]] ["ICN", "JFK", "HND", "IAD"]
[["ICN", "SFO"], ["ICN, "ATL"], ["SFO", "ATL"], ["ATL, "ICN"], ["ATL, "SFO"]] ["ICN", "ATL", "ICN", "SFO", "ATL", "SFO"]

 

 


์ƒ๊ฐํ•ด ์ค„ ๊ฒƒ์ด ์กฐ๊ธˆ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ๋ฌธ์ œ.

BFS๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ฒ ์ง€? ๊ทผ๋ฐ ๋‚œ DFS๋กœ ํ’€์—ˆ๋‹ค.

 

๊ธฐ๋ณธ์ ์ธ ์•„์ด๋””์–ด๋Š”

 

1. ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ•ญ๊ณต๊ถŒ์„ ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ๋‚˜์—ดํ•œ๋‹ค.

2. ICN์„ ์‹œ์ž‘์œผ๋กœ ์—ฌํ–‰ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•ด๋‚˜๊ฐ„๋‹ค.

3. ํƒ์ƒ‰ํ•˜๋ฉฐ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ณตํ•ญ๋“ค์„ ์ €์žฅํ•ด๋‚˜๊ฐ„๋‹ค.

 

์ธ๋ฐ, ์—ฌ๊ธฐ์„œ ๊ณ ๋ คํ•  ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‘ ๊ฐ€์ง€ ์žˆ๋‹ค.

 

1. ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ๊ฒฝ๋กœ ํƒ์ƒ‰ ์‹œ ํ‹ฐ์ผ“์„ ์ „๋ถ€ ์†Œ์ง„ํ•˜์ง€ ์•Š์€ ์ฑ„๋กœ ํƒ์ƒ‰์ด ์ข…๋ฃŒ๋œ ๊ฒฝ์šฐ

    ์˜ˆ: [ICN → HND], [ICN → KIX], [KIX → ICN]

 

2. ์ค‘๋ณต๋œ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ

    ์˜ˆ: [ICN → HND], [JFK → NRT], [HND → JFK], [NRT → ICN], [HND → SFO], [ICN → HND]

 

 

๋”ฐ๋ผ์„œ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ณผ์ • ์ค‘์—

ํ•ด๋‹น ๋…ธ๋“œ์—์„œ ์ถœ๋ฐœํ–ˆ์„ ๋•Œ ํ‹ฐ์ผ“์„ ์ „๋ถ€ ์†Œ์ง„ํ•˜์ง€ ์•Š์€ ์ฑ„๋กœ ์—ฌํ–‰์ด ๋๋‚  ๊ฒฝ์šฐ

ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ์žฌํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ false๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ์ž‘์—…์ด ์ถ”๊ฐ€๋˜๊ณ ,

 

์—ฌํ–‰ ๋ณ„๋กœ ํ‹ฐ์ผ“ ์†Œ์ง„ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ‹ฐ์ผ“ ์ •๋ณด๋กœ๋ถ€ํ„ฐ ๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค.

 

 

๋ณ„๋กœ ํšจ์œจ์ ์ธ ์ฝ”๋“œ๋Š” ์•„๋‹ˆ๊ธด ํ•˜์ง€๋งŒ.. ์ดˆ๊ธฐ ์•„์ด๋””์–ด๋กœ๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ์งฐ๋‹ค..

์ฝ”๋“œ
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

map<string, int> airport;
vector<string> answer_temp;
int ticket_no;
bool is_used = false;
vector<vector<pair<bool, string>>> graph;

void DFS(int arrival, int ticket) {
    if(ticket == ticket_no) is_used = true;

    for(auto& i:graph[arrival - 1]) {
        if(!i.first) {
            i.first = true;
            answer_temp.push_back(i.second);
            DFS(airport[i.second], ++ticket);

            if(!is_used) {
                i.first = false;
                answer_temp.pop_back();
            }
        }
    }
}


vector<string> solution(vector<vector<string>> tickets) {
    vector<string> answer;

    sort(tickets.begin(), tickets.end());

    ticket_no = tickets.size();

    int count = 1;
    for(auto&i : tickets) {
        for(auto& j : i) {
            if(airport[j]) continue;
            airport[j] = count;
            count++;
        }
    }
    
    int airport_count = count - 1;
    graph.resize(airport_count, vector<pair<bool, string>> ());

    for(auto&i : tickets) {
        int arrival = airport[i[0]];

        graph[arrival - 1].push_back(make_pair(false, i[1]));
    }

    answer_temp.push_back("ICN");

    DFS(airport["ICN"], 0);

    answer = answer_temp;
    return answer;
}

 

Comments