프로그래머스 [level 2] 게임 맵 최단거리 - 1844 (BFS)

2025. 11. 5. 23:06·Coding Test/BFS

문제

#include<vector>
#include <deque>
#include <iostream>
#include <algorithm>

using namespace std;

int solution(vector<vector<int> > maps)
{
    int n = maps.size(); 
    int m = maps[0].size(); 

    int i_Count = 0; 

    vector<vector<int>> m_vecVisit(n, vector<int>(m,-1));

    deque<pair<int,int>> m_dequeVisit; 
    m_dequeVisit.push_front({0,0});
    
    m_vecVisit[0][0] = 1; 

    while(!m_dequeVisit.empty())
    {
        int i_PosX = m_dequeVisit.front().first; 
        int i_PosY = m_dequeVisit.front().second; 

        m_dequeVisit.pop_front(); // 빼주기  


        if (i_PosX == n-1 && i_PosY == m-1)
        {
            return  m_vecVisit[i_PosX][i_PosY]; 
        }
        
        // 4 가지 경우 나누기 ( 동 서 남 북 ) 
        if(i_PosX - 1 >= 0)
        {
           if(maps[i_PosX-1][i_PosY] == 1 && m_vecVisit[i_PosX - 1][i_PosY] == -1)
           {
               m_dequeVisit.push_back({ i_PosX - 1 ,i_PosY });
               m_vecVisit[i_PosX - 1][i_PosY] = m_vecVisit[i_PosX][i_PosY] + 1; 
           }
        }

        if (i_PosX + 1 < n)
        {
            if (maps[i_PosX + 1][i_PosY] == 1 && m_vecVisit[i_PosX + 1][i_PosY] == -1)
            {
                m_dequeVisit.push_back({ i_PosX + 1 ,i_PosY });
                m_vecVisit[i_PosX + 1][i_PosY] = m_vecVisit[i_PosX][i_PosY] + 1; 
            }
        }
        

        if (i_PosY - 1 >= 0)
        {
            if (maps[i_PosX][i_PosY -1] == 1 && m_vecVisit[i_PosX][i_PosY - 1] == -1)
            {
                m_dequeVisit.push_back({ i_PosX ,i_PosY - 1 });
                m_vecVisit[i_PosX][i_PosY -1 ] = m_vecVisit[i_PosX][i_PosY] + 1; 
            }
        }


        if (i_PosY + 1 < m)
        {
            if (maps[i_PosX][i_PosY + 1] == 1 && m_vecVisit[i_PosX][i_PosY + 1] == -1)
            {
                m_dequeVisit.push_back({ i_PosX ,i_PosY + 1 });
                m_vecVisit[i_PosX][i_PosY + 1] = m_vecVisit[i_PosX][i_PosY] + 1; 
            }
        }

    }


    return -1; 
}

 

 

문제 접근 순서

 

1. 문제를 보니 동서남북으로 모든 곳을 탐색해야 하고 가장 빠른 길을 찾아야 하므로 BFS 알고리즘이 옳다고 생각함. 

 

2. 구현하기 위해서 모든 경우의 수를 deque 자료구조에 넣고 방문 기록을 vector 2차원 배열로 구현. 

 

3. 인덱스 요소가 혹시라도 범위를 벗어날 수 있으므로 먼저 if문으로 체크를 하기로 생각.

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

프로그래머스[Level2] 게임 맵 최단거리 (BFS 알고리즘)  (0) 2026.02.19
백준 2667 "단지 번호 붙이기" [BFS 알고리즘]  (0) 2026.01.30
프로그래머스 [level3] 가장 먼 노드 ( BFS 알고리즘)  (0) 2025.11.13
백준 1012번, "유기농 배추" ( BFS 알고리즘 )  (0) 2025.11.13
프로그래머스 [Level3] 아이템 줍기  (0) 2025.11.11
'Coding Test/BFS' 카테고리의 다른 글
  • 백준 2667 "단지 번호 붙이기" [BFS 알고리즘]
  • 프로그래머스 [level3] 가장 먼 노드 ( 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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
seonhwan2547
프로그래머스 [level 2] 게임 맵 최단거리 - 1844 (BFS)