프로그래머스(C++) - 여행경로

2025. 10. 5. 18:23·C++ 프로그래머스 다이어리/BFS , DFS

●문제

 

●입출력

 

문제해석

2차원 벡터 ticket 항공편에서 ["출발" "도착"] 항공편에서 무조건 "ICN"으로 시작해서 알파벳순으로 항공편을 RETURN

sort로 정렬한뒤 DFS로 풀이

풀이

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <stdio.h>

using namespace std;

bool dfs(string current, vector<vector<string>>& tickets, vector<bool>& visited, vector<string>& answer, int use_Ticket)
{
    answer.push_back(current);
    if (use_Ticket == tickets.size())
        return true;
    for (int i = 0; i < tickets.size(); i++)
    {
        if (tickets[i][0] == current && !visited[i])
        {
            visited[i] = true;
            bool found = dfs(tickets[i][1], tickets, visited, answer, use_Ticket + 1);
            if (found)
                return true;
            visited[i] = false;
        }
    }
    answer.pop_back();
    return false;
}
vector<string> solution(vector<vector<string>> tickets) 
{
    vector<bool> visited(tickets.size(), false);
    sort(tickets.begin(), tickets.end());
    vector<string> answer;
    dfs("ICN", tickets, visited, answer, 0);
    return answer;
}
int main()
{
    vector<vector<string>>ticket = {
        {"ICN", "JFK"},
        {"HND", "IAD"},
        {"JFK", "HND"}
    };
    vector<string>answer = solution(ticket);
    for (int i = 0; i < answer.size(); i++)
    {
        printf("%s ", answer[i].c_str());
    }
    printf("\n");
    return 0;


}

 

레벨 : 3

점수 : 2

'C++ 프로그래머스 다이어리 > BFS , DFS' 카테고리의 다른 글

프로그래머스(C++) - 단어 변환  (0) 2025.09.23
프로그래머스(C++) - 게임 맵 최단거리  (0) 2025.09.14
프로그래머스(C++) - 네트워크  (0) 2025.09.13
프로그래머스(C++) - 타겟 넘버  (0) 2025.09.13
Algorithm - BFS, DFS  (0) 2025.09.12
'C++ 프로그래머스 다이어리/BFS , DFS' 카테고리의 다른 글
  • 프로그래머스(C++) - 단어 변환
  • 프로그래머스(C++) - 게임 맵 최단거리
  • 프로그래머스(C++) - 네트워크
  • 프로그래머스(C++) - 타겟 넘버
lucodev
lucodev
커피와 노트북 그리고 개발
  • lucodev
    루코 개발테이블
    lucodev
  • 전체
    오늘
    어제
    • 분류 전체보기 (171) N
      • Unreal5 프로젝트 다이어리 (73)
      • Unreal5 프로젝트 다이어리2 (11)
      • Unreal 팁 (8)
      • Unreal 디버깅 (8)
      • 코드 개인보관함 (8)
      • C++ 프로그래머스 다이어리 (49) N
        • Stack,Queue (6)
        • Hash (4)
        • Heap (2)
        • Sort (5)
        • Exhaustive search (5)
        • Greedy (2)
        • BFS , DFS (6)
        • Graph (2)
        • Dynamic Programming (1)
        • C++ Math (2)
        • 기타 문제 (13) N
      • 코딩테스트+@ (10) N
      • 알고리즘 스타디 (1)
      • 알고리즘 스타디 과제 (3)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • 링크

  • 공지사항

  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 태그

    unreal 모션매칭
    언리얼 look at
    언리얼 컷씬
    언리얼 비헤이비어트리
    언리얼 로딩
    언리얼 behaviortree
    unreal loading
    unreal look at
    언리얼 시퀀스
    언리얼 모션매칭
    언리얼 behavior tree
    언리얼 motionmatching
    unreal 로딩
    언리얼
    언리얼 로딩창
    언리얼 페이드 아웃
    언리얼 foot step
    unreal sequence
    unreal 시퀀스
    unreal 컷씬
  • hELLO· Designed By정상우.v4.10.3
lucodev
프로그래머스(C++) - 여행경로
상단으로

티스토리툴바