[Programmers] Lv2. ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ | C++

2023. 6. 9. 02:38ใ†Coding Test/Programmers

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

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

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

programmers.co.kr

 

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

 

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() ํ•จ์ˆ˜๋ฅผ ์ผ์œผ๋ฉด ๋” ๊น”๋”ํ–ˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.

 

 

728x90
๋ฐ˜์‘ํ˜•