[Programmers] Lv2. ๋ฆฌ์ฝ์ณ ๋ก๋ด | C++
2023. 6. 7. 16:57ใCoding Test/Programmers
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
์ฒ์์๋ DFS๋ก ์ ๊ทผ์ ํ๋ค๊ฐ ๊ฒฐ๊ตญ ๋ชป ํ์์ต๋๋ค. ์๋ง ๊ทธ๋๋ก ์งํํ์ด๋ ๊ฐ์ง ์น๊ธฐ๋ฅผ ๋ชปํด์ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฌ์ ๊ฒ ๊ฐ๋ค์. ๊ทธ๋์ BFS๋ก ์ ๋ต์ ๋ฐ๊ฟ ํ๋ค ๋ณด๋ ์๊ฒ ๋ ์ฌ์ค์ธ๋ฐ, ์ ๊ฐ ์ค๊ณ๋ฅผ ์กฐ๊ธ ์๋ชปํ์๋๋ผ๊ตฌ์. ์ฅ์ ๋ฌผ์ด๋ ๋งจ ๋์ ๋ถ๋ชํ ๋น ๊ณต๊ฐ๋ง ๋ฐฉ๋ฌธ ํ์๋ฅผ ํ์์ด์ผ ํ๋๋ฐ, ์ง๋๊ฐ ๊ฒฝ๋ก๋ฅผ ๋ค ๋ฐฉ๋ฌธ ํ์ ํด๋ฒ๋ ธ์ต๋๋ค..ใ ใ ใ
์๋ฌดํผ, BFS๋ฅผ ํตํ ์ ๋ต์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ๋ก๋ด์ ์ฒ์ ์์น 'R', ๋ชฉํ ์ง์ 'G'์ ์ขํ๋ฅผ ์์๋ธ ํ ์ ์ฅํด๋๋ค.
- R์ ์ขํ๋ฅผ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌํ ํ, ํ(Queue)์ ์ฝ์ ํ๋ค.
- ํ๊ฐ ๋น ๋๊น์ง,
- ํ์์ ํ๋ ๊ฐ์ ธ์, ํ์ฌ ์ขํ๊ฐ ๋ชฉํ ์ง์ ์ธ์ง ๊ฒ์ฌํ๋ค.
- ๋ชฉํ ์ง์ ์ด๋ผ๋ฉด, 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
๋ฐ์ํ
'Coding Test > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ์์ ์ต๋ํ | C++ (0) | 2023.06.08 |
---|---|
[Programmers] Lv2. ๋กค์ผ์ดํฌ ์๋ฅด๊ธฐ | C++ (2) | 2023.06.08 |
[Programmers] Lv2. ๊ด๋ฌผ ์บ๊ธฐ | C++ (0) | 2023.06.06 |
[Programmers] Lv2. ๋ง๋ฒ์ ์๋ฆฌ๋ฒ ์ดํฐ | C++ (0) | 2023.06.05 |
[Programmers] Lv2. ๋ค์ ์๋ ํฐ ์ ์ฐพ๊ธฐ | C++ (0) | 2023.06.04 |