728x90
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
์ฒ์์ 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
๋ฐ์ํ
'๐คAlgorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ๋ชจ์ ์ฌ์ | C++ (0) | 2023.06.04 |
---|---|
[Programmers] Lv2. ํธํ ๋์ค (0) | 2023.06.03 |
[Programmers] Lv2. ๋ฌด์ธ๋ ์ฌํ | C++ (0) | 2023.06.03 |
[Programmers] Lv0. ๊ตฌ์ฌ์ ๋๋๋ ๊ฒฝ์ฐ์ ์ | C++ (0) | 2023.02.08 |
[Programmers] Lv0. ์ต๋น๊ฐ ๊ตฌํ๊ธฐ | C++ (0) | 2023.01.29 |