[Programmers] Lv2. ๋‹จ์ฒด์‚ฌ์ง„ ์ฐ๊ธฐ | C++

2023. 8. 12. 12:43ใ†Coding Test/Programmers

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

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

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

programmers.co.kr

 

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

 

์ €๋Š” DFS๋ฅผ ์ด์šฉํ•˜์—ฌ 8๋ช…์˜ ์บ๋ฆญํ„ฐ๋“ค์˜ ์ˆœ์—ด๋งˆ๋‹ค ์กฐ๊ฑด์‹์„ ๊ฒ€์‚ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•œ ๊ฐ€์ง€ ์‹ค์ˆ˜ํ–ˆ๋˜ ๊ฒŒ, ์กฐ๊ฑด์‹์˜ ๋‹ค์„ฏ๋ฒˆ์งธ ๊ธ€์ž์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ„๊ฒฉ์€ ๋‘ ์บ๋ฆญํ„ฐ ์‚ฌ์ด์˜ ์บ๋ฆญํ„ฐ ์ˆซ์ž๋ผ๋Š” ๊ฑธ ๊นŒ๋จน์—ˆ์Šต๋‹ˆ๋‹ค. ์ €์ฒ˜๋Ÿผ ์ธ๋ฑ์Šค ํƒ์ƒ‰ ๋ฐฉ์‹์œผ๋กœ ํ•  ์‹œ, ๊ฐ„๊ฒฉ์—๋‹ค๊ฐ€ -1์„ ํ•ด์ฃผ๋Š” ์ถ”๊ฐ€ ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

int distance = abs(sourceIndex - targetIndex) - 1;

 

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

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const vector<char> friends{ 'A', 'C', 'F', 'J', 'M', 'N', 'R', 'T' };
vector<bool> used(friends.size(), false);

struct Constraint
{
    Constraint() { }
    Constraint(char source, char target, char operator_, int distance) : _source(source), _target(target), _operator(operator_), _distance(distance) { }
    char _source;
    char _target;
    char _operator;
    int  _distance;
};

bool IsOk(int difference, int distance, char _operator)
{
    switch (_operator)
    {
    case '=':
        return difference == distance;
    case '<':
        return difference < distance;
    case '>':
        return difference > distance;
    }

    return false;
}

void DFS(const vector<Constraint>& constraints, vector<char>& sequence, int& answer)
{
    if (sequence.size() == friends.size())
    {
        bool isOk = true;

        for (const auto& constraint : constraints)
        {
            int sourceIndex = std::distance(sequence.begin(), std::find(sequence.begin(), sequence.end(), constraint._source));
            int targetIndex = std::distance(sequence.begin(), std::find(sequence.begin(), sequence.end(), constraint._target));
            int difference = abs(sourceIndex - targetIndex) - 1;

            if (!IsOk(difference, constraint._distance, constraint._operator))
            {
                isOk = false;
                break;
            }
        }

        if (isOk)
            answer++;
        return;
    }

    for (int i = 0; i < friends.size(); i++)
    {
        if (used[i])
            continue;

        sequence.push_back(friends[i]);
        used[i] = true;

        DFS(constraints, sequence, answer);
        used[i] = false;
        sequence.pop_back();
    }
}

int solution(int n, vector<string> data) {
    int answer = 0;
    vector<Constraint> constrains;
    vector<char> sequence;

    for (const auto& constraint : data)
        constrains.emplace_back(Constraint(constraint[0], constraint[2], constraint[3], constraint[4] - '0'));

    DFS(constrains, sequence, answer);
    return answer;
}

 

 

 

vector<char>๋ง๊ณ  ๊ทธ๋ƒฅ string์œผ๋กœ ์‚ฌ์šฉํ–ˆ์—ˆ์–ด๋„ ๋  ๊ฒƒ ๊ฐ™๋„ค์š”. ์บ๋ฆญํ„ฐ๋“ค์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ‘œํ˜„ ๋˜ํ•œ ๋ฌธ์ž์ด๋ฏ€๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ˆ, next_permutation() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์œผ๋ฉด ๋” ๊น”๋”ํ–ˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•