๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

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

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

programmers.co.kr

 

 

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

 

์ฒ˜์Œ์—” DFS๋กœ ์ ‘๊ทผํ–ˆ์ง€๋งŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋–ด์Šต๋‹ˆ๋‹ค. ์‹œ๊ฐ„์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ๋ชฉํ‘œ(๋ ˆ๋ฒ„ ํ˜น์€ ์ถœ๊ตฌ) ์ง€์  ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๊ฒŒ๋” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋‹ค์‹œ ์งฐ์ง€๋งŒ, ๊ฐ€๋Š” ๊ธธ๋ชฉ์ด ๋ง‰ํ˜€์„œ ๋‹ค๋ฅธ ๊ธธ๋กœ ๋Œ์•„๊ฐ€์•ผ ํ•  ๋•Œ ๋ฐฉํ–ฅ์ด ๋‹ฌ๋ผ์ ธ DFS ํƒ์ƒ‰์ด ๋๋‚˜๋ฒ„๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ณ ๋ฏผ์„ ๋งŽ์ด ํ•ด๋ดค์ง€๋งŒ ๋‹ต์ด ๋ณด์ด์ง€ ์•Š์•„ ํ•ด๋‹น ์ „๋žต์€ ํฌ๊ธฐํ•˜๊ณ , BFS๋กœ ๊ฐˆ์•„ํƒ€์„œ ํ’€๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. BFS๋กœ ํ‘ผ ์ ์ด ๋ณ„๋กœ ์—†์–ด ์ฝ”๋“œ ์ž‘์„ฑํ•˜๋Š” ๊ฒŒ ์กฐ๊ธˆ ์–ด์ƒ‰ํ–ˆ๋Š”๋ฐ, ์ด ์ฐธ์— ํ•œ ๋ฒˆ ์—ฐ์Šตํ•ด ๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

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

#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int rowDirection[4] = { -1, 1, 0, 0 };
int columnDirection[4] = { 0, 0, -1, 1 };

class Info
{
public:
    Info(int row, int column, int currentTime) : _row(row), _column(column), _currentTime(currentTime) { }
    int _row;
    int _column;
    int _currentTime;
};

map<char, pair<int, int>> GetLabelPositions(const vector<string>& maps)
{
    map<char, pair<int, int>> result;

    for (int row = 0; row < maps.size(); row++)
    {
        for (int column = 0; column < maps[row].length(); column++)
        {
            char label = '@';

            if (maps[row][column] == 'S')
                label = 'S';
            else if (maps[row][column] == 'E')
                label = 'E';
            else if (maps[row][column] == 'L')
                label = 'L';

            if (label != '@')
                result.insert(make_pair(label, make_pair(row, column)));
        }
    }

    return result;
}


int CalculateTimeWithBFS(const vector<string>& maps, Info start, Info end, bool& isFindTarget)
{
    vector<vector<bool>> visited(maps.size(), vector<bool>(maps[0].length(), false));
    int minTime = 2147483647;
    queue<Info> _queue;

    _queue.push(start);
    visited[start._row][start._column] = true;

    while (!_queue.empty())
    {
        Info current = _queue.front();
        _queue.pop();

        if ((current._row == end._row) and (current._column == end._column))
        {
            isFindTarget = true;
            minTime = min(minTime, current._currentTime);
        }

        for (int i = 0; i < 4; i++)
        {
            int nextRow = current._row + rowDirection[i];
            int nextColumn = current._column + columnDirection[i];

            if (nextRow < 0 or nextRow >= maps.size() or nextColumn < 0 or nextColumn >= maps[0].length())
                continue;
            if (visited[nextRow][nextColumn] or maps[nextRow][nextColumn] == 'X')
                continue;

            _queue.push(Info(nextRow, nextColumn, current._currentTime + 1));
            visited[nextRow][nextColumn] = true;
        }
    }

    if (!isFindTarget)
        return -1;
    return minTime;
}


int FindMinEscapeMazeTime(map<char, pair<int, int>>& labelPositions, const vector<string>& maps)
{
    int result = 0;
    bool isFindTarget = false;
    auto start = labelPositions['S'];
    auto lever = labelPositions['L'];
    auto exit = labelPositions['E'];

    int findingLeverMinTime = CalculateTimeWithBFS(maps, Info(start.first, start.second, 0), Info(lever.first, lever.second, 0), isFindTarget);
    if (findingLeverMinTime == -1)
        return -1;


    isFindTarget = false;
    int findingExitMinTime = CalculateTimeWithBFS(maps, Info(lever.first, lever.second, 0), Info(exit.first, exit.second, 0), isFindTarget);
    if (findingExitMinTime == -1)
        return -1;

    return findingLeverMinTime + findingExitMinTime;
}

int solution(vector<string> maps) {
    map<char, pair<int, int>> labelPositions = GetLabelPositions(maps);
    int answer = FindMinEscapeMazeTime(labelPositions, maps);
    return answer;
}

 

์ œ์ถœ ๊ฒฐ๊ณผ

 

728x90
๋ฐ˜์‘ํ˜•