[Programmers] Lv2. ๋ฆฌ์ฝ”์ณ‡ ๋กœ๋ด‡ | C++

2023. 6. 7. 16:57ใ†Coding Test/Programmers

๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

์ฒ˜์Œ์—๋Š” DFS๋กœ ์ ‘๊ทผ์„ ํ–ˆ๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ๋ชป ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ์•„๋งˆ ๊ทธ๋Œ€๋กœ ์ง„ํ–‰ํ–ˆ์–ด๋„ ๊ฐ€์ง€ ์น˜๊ธฐ๋ฅผ ๋ชปํ•ด์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์„ ๊ฒƒ ๊ฐ™๋„ค์š”. ๊ทธ๋ž˜์„œ BFS๋กœ ์ „๋žต์„ ๋ฐ”๊ฟ” ํ’€๋‹ค ๋ณด๋‹ˆ ์•Œ๊ฒŒ ๋œ ์‚ฌ์‹ค์ธ๋ฐ, ์ œ๊ฐ€ ์„ค๊ณ„๋ฅผ ์กฐ๊ธˆ ์ž˜๋ชปํ–ˆ์—ˆ๋”๋ผ๊ตฌ์š”. ์žฅ์• ๋ฌผ์ด๋‚˜ ๋งจ ๋์— ๋ถ€๋”ชํžŒ ๋นˆ ๊ณต๊ฐ„๋งŒ ๋ฐฉ๋ฌธ ํ‘œ์‹œ๋ฅผ ํ–ˆ์—ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ, ์ง€๋‚˜๊ฐ„ ๊ฒฝ๋กœ๋ฅผ ๋‹ค ๋ฐฉ๋ฌธ ํ‘œ์‹œ ํ•ด๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค..ใ…Žใ…Žใ…Ž

 

์•„๋ฌดํŠผ, BFS๋ฅผ ํ†ตํ•œ ์ „๋žต์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

  1. ๋กœ๋ด‡์˜ ์ฒ˜์Œ ์œ„์น˜ 'R', ๋ชฉํ‘œ ์ง€์  'G'์˜ ์ขŒํ‘œ๋ฅผ ์•Œ์•„๋‚ธ ํ›„ ์ €์žฅํ•ด๋‘”๋‹ค.
  2. R์˜ ์ขŒํ‘œ๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌํ•œ ํ›„, ํ(Queue)์— ์‚ฝ์ž…ํ•œ๋‹ค.
  3. ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€,
    • ํ์—์„œ ํ•˜๋‚˜ ๊ฐ€์ ธ์™€, ํ˜„์žฌ ์ขŒํ‘œ๊ฐ€ ๋ชฉํ‘œ ์ง€์ ์ธ์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.
      • ๋ชฉํ‘œ ์ง€์ ์ด๋ผ๋ฉด, min(์ด์ „ ํƒ์ƒ‰๊ฐ’, ํ˜„์žฌ ํƒ์ƒ‰๊ฐ’)์„ ์ง„ํ–‰ํ•œ๋‹ค.
    • ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.
      • ์žฅ์• ๋ฌผ์ด๋‚˜ ๋งจ ๋ ๋ฒฝ์ด ์•„๋‹ˆ๋ผ๋ฉด ๊ณ„์† ํ•ด๋‹น ๋ฐฉํ–ฅ์œผ๋กœ ์ง„ํ–‰ํ•œ๋‹ค.
      • ์žฅ์• ๋ฌผ์ด๋‚˜ ๋งจ ๋ ๋ฒฝ์— ๋ง‰ํžˆ๊ณ , ์ด์ „์— ๋ฐฉ๋ฌธํ•œ ์ขŒํ‘œ๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ, ํ์— ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.
      • ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int MAX_INT = 2147483647;
vector<pair<int, int>> directions {{-1, 0}, { 1, 0 }, { 0, -1 }, { 0, 1 }};   // ์ƒ, ํ•˜, ์ขŒ, ์šฐ

struct Info
{
    Info(int row, int col, int currentMoveCount) : _row(row), _col(col), _currentMoveCount(currentMoveCount) { }
    int _row;
    int _col;
    int _currentMoveCount;
};


int FindShortestDistance(const vector<string>& board, pair<int, int> current, const pair<int, int>& end) {
    int minCount = MAX_INT;

    queue<Info> myQueue;
    vector<vector<bool>> visited(board.size(), vector<bool>(board[0].length(), false));
    
    myQueue.push(Info(current.first, current.second, 0));
    visited[current.first][current.second] = true;


    while (!myQueue.empty()) {
        Info current = myQueue.front();
        myQueue.pop();
        bool isArrivalGoal = (current._row == end.first) and (current._col == end.second);

        if (isArrivalGoal)
            minCount = min(minCount, current._currentMoveCount);


        for (int i = 0; i < directions.size(); i++) {

            int nextRow = current._row + directions[i].first;
            int nextCol = current._col + directions[i].second;
            bool isBlocked = nextRow < 0 or nextRow >= board.size() or nextCol < 0 or nextCol >= board[0].length() or board[nextRow][nextCol] == 'D';

            if (isBlocked)
                continue;

            int tempNextRow = nextRow;
            int tempNextCol = nextCol;

            while (true) {
                tempNextRow += directions[i].first;
                tempNextCol += directions[i].second;
                isBlocked = tempNextRow < 0 or tempNextRow >= board.size() or tempNextCol < 0 or tempNextCol >= board[0].length() or board[tempNextRow][tempNextCol] == 'D';

                if (isBlocked)
                    break;
                nextRow += directions[i].first;
                nextCol += directions[i].second;
            }

            if (visited[nextRow][nextCol])
                continue;
            
            myQueue.push(Info(nextRow, nextCol, current._currentMoveCount + 1));
            visited[nextRow][nextCol] = true;
        }
    }

    return minCount;
}


int FindMinMovingCount(const vector<string>& board, map<char, pair<int, int>> symbolPositions) {
    auto currentRobotPosition = symbolPositions['R'];
    auto goal = symbolPositions['G'];

    int answer = FindShortestDistance(board, currentRobotPosition, goal);
    return answer == MAX_INT ? -1 : answer;
}

map<char, pair<int, int>> GetPositions(const vector<string>& board) {
    map<char, pair<int, int>> results;

    for (int row = 0; row < board.size(); row++) {
        for (int col = 0; col < board[row].length(); col++) {
            char symbol = '@';

            if (board[row][col] == 'R')
                symbol = 'R';
            else if (board[row][col] == 'G')
                symbol = 'G';

            if (symbol != '@')
                results.insert(make_pair(symbol, make_pair(row, col)));
        }
    }

    return results;
}


int solution(vector<string> board) {
    map<char, pair<int, int>> symbolPositions = GetPositions(board);
    int answer = FindMinMovingCount(board, symbolPositions);

    return answer;
}

 

์ œ์ถœ ๊ฒฐ๊ณผ

 

 

728x90
๋ฐ˜์‘ํ˜•