2023. 6. 9. 02:38ใCoding Test/Programmers
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
1. ์ฃผ์ด์ง ๋๊ธฐ์ค์ ์ํํ๋ฉฐ, ๊ฐ ์์์๋ค ์ขํ(ํ, ์ด)๊ฐ ์ ์ฅํ๊ธฐ
2. ์ ์ฅํ ์์์๋ค ์ขํ๋ฅผ ์ค๋ณต ์์ด 2๊ฐ ๋ฝ์, ๊ฑฐ๋ฆฌ๋๊ธฐ ์ ๋ฌด ๊ฒ์ฌํ๊ธฐ
- ๋งจํดํผ ๊ฑฐ๋ฆฌ๊ฐ 2๋ณด๋ค ํด ๊ฒฝ์ฐ์๋ ๊ฑฐ๋ฆฌ ๋๊ธฐ๋ฅผ ์ ์งํค๊ณ ์๋ค๊ณ ํ์
- ๋งจํดํผ ๊ฑฐ๋ฆฌ๊ฐ 2๋ณด๋ค ์์ ๊ฒฝ์ฐ์๋ ๊ฑฐ๋ฆฌ ๋๊ธฐ๋ฅผ ์งํค์ง ์๊ณ ์๋ค๊ณ ํ์
- ๋งจํดํผ ๊ฑฐ๋ฆฌ๊ฐ 2์ผ ๊ฒฝ์ฐ (P1, P2๊ฐ ์๋ค๊ณ ๊ฐ์ )
- ๊ฐ๋ก๋ก ํ ์นธ ๋์ด์ ธ ์๋ ๊ฒฝ์ฐ : P1.column + 1 ๋ถ๋ถ์ ๊ฒ์ฌ (P1.column < P2.column)
- ์ธ๋ก๋ก ํ ์นธ ๋์ด์ ธ ์๋ ๊ฒฝ์ฐ : P1.row + 1 ๋ถ๋ถ์ ๊ฒ์ฌ (P1.row < P2.row)
- ๋๊ฐ์ ์ผ๋ก ํ ์นธ ๋์ด์ ธ ์๋ ๊ฒฝ์ฐ : ์ ์ ์ขํ๋ ๊ฐ๊ฐ [P1.row][P2.column], [P2.row][P1.column]
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <string>
#include <vector>
using namespace std;
const char CANDIDATE = 'P';
const char PARTITION = 'X';
const int MAX_MANHATTAN_DISTANCE = 2;
vector<pair<int, int>> GetCandidatePositions(const vector<string>& waitingRoom) {
vector<pair<int, int>> results;
for (int row = 0; row < waitingRoom.size(); row++) {
for (int col = 0; col < waitingRoom[0].length(); col++) {
if (waitingRoom[row][col] == CANDIDATE)
results.push_back(make_pair(row, col));
}
}
return results;
}
int CalculateManhattanDistance(pair<int, int> pos1, pair<int, int> pos2) {
return abs(pos1.first - pos2.first) + abs(pos1.second - pos2.second);
}
void Combination(const vector<string>& waitingRoom, const vector<pair<int, int>>& candidatePositions, vector<pair<int, int>>& candidates, int currentIdx, bool& result) {
if (candidates.size() == 2) {
int manhattanDistance = CalculateManhattanDistance(candidates[0], candidates[1]);
if (manhattanDistance > MAX_MANHATTAN_DISTANCE)
return;
if (manhattanDistance < MAX_MANHATTAN_DISTANCE) {
result = false;
return;
}
if (manhattanDistance == MAX_MANHATTAN_DISTANCE) {
bool isEqualRow = candidates[0].first == candidates[1].first;
bool isEqualCol = candidates[0].second == candidates[1].second;
int minRow = isEqualRow ? -1 : candidates[0].first < candidates[1].first ? candidates[0].first : candidates[1].first;
int minCol = isEqualCol ? -1 : candidates[0].second < candidates[1].second ? candidates[0].second : candidates[1].second;
// ๊ฐ๋ก๋ก ํ ์นธ ๋์ด์ ธ ์๋ ๊ฒฝ์ฐ
if (isEqualRow and minCol != -1) {
int row = candidates[0].first;
if (waitingRoom[row][minCol + 1] != PARTITION)
result = false;
return;
}
// ์ธ๋ก๋ก ํ ์นธ ๋์ด์ ธ ์๋ ๊ฒฝ์ฐ
if (isEqualCol and minRow != -1) {
int col = candidates[0].second;
if (waitingRoom[minRow + 1][col] != PARTITION)
result = false;
return;
}
// ๋๊ฐ์ ์ผ๋ก ํ ์นธ ๋์ด์ ธ ์๋ ๊ฒฝ์ฐ (r1, c1), (r2, c2)๋ผ๊ณ ํ๋ค๋ฉด,
// ์ ์ ํ
์ด๋ธ ์ขํ๋ [r1][c2], [r2][c1]
if ((!isEqualRow and !isEqualCol) and (waitingRoom[candidates[0].first][candidates[1].second] != PARTITION or waitingRoom[candidates[1].first][candidates[0].second] != PARTITION)) {
result = false;
}
}
return;
}
for (int i = currentIdx; i < candidatePositions.size(); i++) {
if (!result)
break;
candidates.push_back(candidatePositions[i]);
Combination(waitingRoom, candidatePositions, candidates, i + 1, result);
candidates.pop_back();
}
}
bool DoWellCovidDistance(const vector<string>& waitingRoom, const vector<pair<int, int>>& candidatePositions) {
bool result = true;
vector<pair<int, int>> candidates;
Combination(waitingRoom, candidatePositions, candidates, 0, result);
return result;
}
vector<int> solution(vector<vector<string>> places) {
vector<int> answer;
for (const auto waitingRoom : places) {
vector<pair<int, int>> candidatePositions = GetCandidatePositions(waitingRoom);
bool doWellCovidDistance = DoWellCovidDistance(waitingRoom, candidatePositions);
answer.push_back(doWellCovidDistance);
}
return answer;
}
๋๊ฐ์ ์ ์ ๊ฒ์ฌ๊ฐ ๊น๋ค๋ก์ ๋ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค. ๋ค ์ ๊ณ ๋์ ๊นจ๋ฌ์ ๊ฑฐ์ง๋ง, minRow์ minCol์ ๊ตฌํ ๋ min() ํจ์๋ฅผ ์ผ์ผ๋ฉด ๋ ๊น๋ํ์ ๊ฒ ๊ฐ๋ค์.
'Coding Test > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ์ต์๊ฐ ๋ง๋ค๊ธฐ | C++ (1) | 2023.06.11 |
---|---|
[Programmers] Lv2. JadenCase ๋ฌธ์์ด ๋ง๋ค๊ธฐ | C++ (0) | 2023.06.11 |
[Programmers] Lv2. ์์ ์ต๋ํ | C++ (0) | 2023.06.08 |
[Programmers] Lv2. ๋กค์ผ์ดํฌ ์๋ฅด๊ธฐ | C++ (2) | 2023.06.08 |
[Programmers] Lv2. ๋ฆฌ์ฝ์ณ ๋ก๋ด | C++ (0) | 2023.06.07 |