프로그래머스 [level3] 가장 먼 노드 ( BFS 알고리즘)

2025. 11. 13. 22:33·Coding Test/BFS

문제

#include <string>
#include <vector>
#include <deque>
#include <algorithm>

using namespace std;

int solution(int n, vector<vector<int>> edge) 
{
    int answer = 0;
   
    deque<int> m_deque; 
    vector<vector<int>> m_graph(n+1);
    vector<int> m_vecDist(n+1,-1);
    


    for(auto& iter : edge)
    {
        m_graph[iter[0]].push_back(iter[1]);
        m_graph[iter[1]].push_back(iter[0]);        
    }
    
    
    m_deque.push_back(1); 
    m_vecDist[1] = 0; 
    
    
    while(!m_deque.empty())
    {
        int CurNode = m_deque.front();
        m_deque.pop_front(); 
        
        for(auto& iter : m_graph[CurNode])
        {
            if(m_vecDist[iter] == -1)
            {
                m_vecDist[iter] = m_vecDist[CurNode] +1; 
                m_deque.push_back(iter);
            }
        }   
    }
    
    
    sort(m_vecDist.begin(), m_vecDist.end());
    
    for(auto& iter : m_vecDist)
    {
        if(iter == m_vecDist[m_vecDist.size()-1])
            answer++;
    }
    
    
    return answer;
}

 

문제 접근 순서

 

1. 일단 최단경로로 이동 했을 때 가장 긴 거리에 있는 노드가 몇개 있느냐를 목적으로 하는 문제로 

   처음 문제를 보자마자 아 이거 너비우선탐색 (BFS) 이구나 라고 생각했다.

   그 이유는 어떠한 시작점을 기준으로 최단거리로 가장 먼 곳이라는 목적지에 도착해야하므로 

   너비우선탐색을 통한 가장 먼곳에 도착하는 경우가 즉 최단경로로 갔다라는 의미니깐!

 

2. 여기서 중요한점은 양방향 그래프 이라는 점이다. 

    양방향 그래프 이기 때문에 각 노드마다 연결되어 있는 노드에 대한 정보를 넣어줘야한다.

    한쪽 방향으로만 정보를 넣어주면 BFS 탐색이 불가능하기 때문이다.

 

3. 따라서 BFS 구조이므로 while문과 queue 자료형을 사용하여 코드를 완성시켰다. 

'Coding Test > BFS' 카테고리의 다른 글

프로그래머스[Level2] 게임 맵 최단거리 (BFS 알고리즘)  (0) 2026.02.19
백준 2667 "단지 번호 붙이기" [BFS 알고리즘]  (0) 2026.01.30
백준 1012번, "유기농 배추" ( BFS 알고리즘 )  (0) 2025.11.13
프로그래머스 [Level3] 아이템 줍기  (0) 2025.11.11
프로그래머스 [level 2] 게임 맵 최단거리 - 1844 (BFS)  (0) 2025.11.05
'Coding Test/BFS' 카테고리의 다른 글
  • 프로그래머스[Level2] 게임 맵 최단거리 (BFS 알고리즘)
  • 백준 2667 "단지 번호 붙이기" [BFS 알고리즘]
  • 백준 1012번, "유기농 배추" ( BFS 알고리즘 )
  • 프로그래머스 [Level3] 아이템 줍기
seonhwan2547
seonhwan2547
seonhwan2547 님의 블로그 입니다.
  • seonhwan2547
    seonhwan2547 님의 블로그
    seonhwan2547
  • 전체
    오늘
    어제
    • 분류 전체보기 (80)
      • Unreal Project (17)
        • Khazan 모작 프로젝트 (2)
        • Unreal Study (10)
        • Blueprint (5)
      • Directx11 Project (11)
        • Thymesia 팀 프로젝트 (8)
        • Kaku Ancient Seal 개인 프로젝트 (2)
        • Thymesia Animation Tool 개발 (0)
      • Algorithm (6)
        • Binary_Search (2)
        • Greedy (1)
        • Dynamic Programming (1)
        • A-star (1)
      • Coding Test (31)
        • Brutal Force (2)
        • Sort (5)
        • DFS (3)
        • Binary_Search (4)
        • BFS (6)
        • Hash (2)
        • Dynamic Programming (6)
        • Greedy (1)
        • BackTracking (1)
        • Binary_Tree (1)
      • STL Container (1)
        • unorded_set (0)
        • priority_queue (1)
      • C++ 공부 및 몰랐던점 (8)
        • Smart pointer (1)
      • Visual Studio 설정관련 공부 (1)
      • Console Project (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 블로그 소개
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    GameProgramming
    Unreal
    hash
    Game Programming
    Unreal Engine
    blueprint
    CodingTest
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
seonhwan2547
프로그래머스 [level3] 가장 먼 노드 ( BFS 알고리즘)