Programmers, ๋‹จ์ฒด์‚ฌ์ง„ ์ฐ๊ธฐ

2021. 12. 2. 17:34ใ†ETC/Algorithm

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

 

๋ฌธ์ œ ์„ค๋ช…

 

 

๊ฐ€์„์„ ๋งž์•„ ์นด์นด์˜คํ”„๋ Œ์ฆˆ๋Š” ๋‹จ์ฒด๋กœ ์†Œํ’์„ ๋– ๋‚ฌ๋‹ค. ์ฆ๊ฑฐ์šด ์‹œ๊ฐ„์„ ๋ณด๋‚ด๊ณ  ๋งˆ์ง€๋ง‰์— ๋‹จ์ฒด์‚ฌ์ง„์„ ์ฐ๊ธฐ ์œ„ํ•ด ์นด๋ฉ”๋ผ ์•ž์— ์ผ๋ ฌ๋กœ ๋‚˜๋ž€ํžˆ ์„ฐ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ฐ์ž๊ฐ€ ์›ํ•˜๋Š” ๋ฐฐ์น˜๊ฐ€ ๋ชจ๋‘ ๋‹ฌ๋ผ ์–ด๋–ค ์ˆœ์„œ๋กœ ์„ค์ง€ ์ •ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค. ๋„ค์˜ค๋Š” ํ”„๋กœ๋„์™€ ๋‚˜๋ž€ํžˆ ์„œ๊ธฐ๋ฅผ ์›ํ–ˆ๊ณ , ํŠœ๋ธŒ๊ฐ€ ๋ฟœ์€ ๋ถˆ์„ ๋งž์€ ์ ์ด ์žˆ๋˜ ๋ผ์ด์–ธ์€ ํŠœ๋ธŒ์—๊ฒŒ์„œ ์ ์–ด๋„ ์„ธ ์นธ ์ด์ƒ ๋–จ์–ด์ ธ์„œ ์„œ๊ธฐ๋ฅผ ์›ํ–ˆ๋‹ค. ์‚ฌ์ง„์„ ์ฐ๊ณ  ๋‚˜์„œ ๋Œ์•„์˜ค๋Š” ๊ธธ์—, ๋ฌด์ง€๋Š” ๋ชจ๋‘๊ฐ€ ์›ํ•˜๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด์„œ๋„ ๋‹ค๋ฅด๊ฒŒ ์„œ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์ง€ ์•Š์•˜์„๊นŒ ์ƒ๊ฐํ•ด๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ฐ ํ”„๋ Œ์ฆˆ๊ฐ€ ์›ํ•˜๋Š” ์กฐ๊ฑด์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•˜์„ ๋•Œ ๋ชจ๋“  ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ์ˆ˜ ์žˆ๋„๋ก ์„œ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด๋ณด์ž.

 

 

์ž…๋ ฅ ํ˜•์‹

์ž…๋ ฅ์€ ์กฐ๊ฑด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ n๊ณผ n๊ฐœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด ๋ฐฐ์—ด data๋กœ ์ฃผ์–ด์ง„๋‹ค. data์˜ ์›์†Œ๋Š” ๊ฐ ํ”„๋ Œ์ฆˆ๊ฐ€ ์›ํ•˜๋Š” ์กฐ๊ฑด์ด N~F=0๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. ์ œํ•œ์กฐ๊ฑด์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • 1 <= n <= 100
  • data์˜ ์›์†Œ๋Š” ๋‹ค์„ฏ ๊ธ€์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์ด๋‹ค. ๊ฐ ์›์†Œ์˜ ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    • ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž์™€ ์„ธ ๋ฒˆ์งธ ๊ธ€์ž๋Š” ๋‹ค์Œ 8๊ฐœ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. {A, C, F, J, M, N, R, T} ๊ฐ๊ฐ ์–ดํ”ผ์น˜, ์ฝ˜, ํ”„๋กœ๋„, ์ œ์ด์ง€, ๋ฌด์ง€, ๋„ค์˜ค, ๋ผ์ด์–ธ, ํŠœ๋ธŒ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋Š” ์กฐ๊ฑด์„ ์ œ์‹œํ•œ ํ”„๋ Œ์ฆˆ, ์„ธ ๋ฒˆ์งธ ๊ธ€์ž๋Š” ์ƒ๋Œ€๋ฐฉ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž์™€ ์„ธ ๋ฒˆ์งธ ๊ธ€์ž๋Š” ํ•ญ์ƒ ๋‹ค๋ฅด๋‹ค.
    • ๋‘ ๋ฒˆ์งธ ๊ธ€์ž๋Š” ํ•ญ์ƒ ~์ด๋‹ค.
    • ๋„ค ๋ฒˆ์งธ ๊ธ€์ž๋Š” ๋‹ค์Œ 3๊ฐœ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. {=, <, >} ๊ฐ๊ฐ ๊ฐ™์Œ, ๋ฏธ๋งŒ, ์ดˆ๊ณผ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
    • ๋‹ค์„ฏ ๋ฒˆ์งธ ๊ธ€์ž๋Š” 0 ์ด์ƒ 6 ์ดํ•˜์˜ ์ •์ˆ˜์˜ ๋ฌธ์žํ˜•์ด๋ฉฐ, ์กฐ๊ฑด์— ์ œ์‹œ๋˜๋Š” ๊ฐ„๊ฒฉ์„ ์˜๋ฏธํ•œ๋‹ค. ์ด๋•Œ ๊ฐ„๊ฒฉ์€ ๋‘ ํ”„๋ Œ์ฆˆ ์‚ฌ์ด์— ์žˆ๋Š” ๋‹ค๋ฅธ ํ”„๋ Œ์ฆˆ์˜ ์ˆ˜์ด๋‹ค.

์ถœ๋ ฅ ํ˜•์‹

๋ชจ๋“  ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…์ถœ๋ ฅ


n data answer
2 ["N~F=0", "R~T>2"] 3648
2 ["M~C<2", "C~M>1"] 0

 

์˜ˆ์ œ์— ๋Œ€ํ•œ ์„ค๋ช…

 

์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ๋Š” ๋ฌธ์ œ์— ์„ค๋ช…๋œ ๋ฐ”์™€ ๊ฐ™์ด, ๋„ค์˜ค๋Š” ํ”„๋กœ๋„์™€์˜ ๊ฐ„๊ฒฉ์ด 0์ด๊ธฐ๋ฅผ ์›ํ•˜๊ณ  ๋ผ์ด์–ธ์€ ํŠœ๋ธŒ์™€์˜ ๊ฐ„๊ฒฉ์ด 2๋ณด๋‹ค ํฌ๊ธฐ๋ฅผ ์›ํ•˜๋Š” ์ƒํ™ฉ์ด๋‹ค.

๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ๋Š” ๋ฌด์ง€๊ฐ€ ์ฝ˜๊ณผ์˜ ๊ฐ„๊ฒฉ์ด 2๋ณด๋‹ค ์ž‘๊ธฐ๋ฅผ ์›ํ•˜๊ณ , ๋ฐ˜๋Œ€๋กœ ์ฝ˜์€ ๋ฌด์ง€์™€์˜ ๊ฐ„๊ฒฉ์ด 1๋ณด๋‹ค ํฌ๊ธฐ๋ฅผ ์›ํ•˜๋Š” ์ƒํ™ฉ์ด๋‹ค. ์ด๋Š” ๋™์‹œ์— ๋งŒ์กฑํ•  ์ˆ˜ ์—†๋Š” ์กฐ๊ฑด์ด๋ฏ€๋กœ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 0์ด๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ

[C++ ] next_permutation

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

bool check(char ine, int dis, int cond) {
    if (ine == '=') {
        return dis == cond;
    } else if (ine == '>') {
        return dis > cond;
    } else {
        return dis < cond;
    }
}

int solution(int n, vector<string> data) {
    int answer = 0;
    string friends = "ACFJMNRT";
    
    do {
        for (string d: data) {
            char f1 = d[0], f2 = d[2];
            n = d[4] - '0';
            int dist = friends.find(f1) - friends.find(f2);
            if (!check(d[3], abs(dist)-1, n)) goto next;
        }
        answer++;
        next: {}
    } while (next_permutation(friends.begin(), friends.end()));
    
    return answer;
}

int main() {
    int result = solution(2, {"N~F=0", "R~T>2"});
    cout << "result : " << result << endl;
    return 0;
}

 

next_permutation ์„ ์‚ฌ์šฉํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

ํ†ต๊ณผ (406.76ms, 4.26MB)

 

 

[C++ ] DFS - Permutation

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#define FRIEND 8
using namespace std;

bool visited[FRIEND];
vector<char> selected;
string friends;
int answer;

bool check(char ine, int dis, int cond) {
    if (ine == '=') {
        return dis == cond;
    } else if (ine == '>') {
        return dis > cond;
    } else {
        return dis < cond;
    }
}

bool isValid(vector<string> data, string friends) {
    for (string d: data) {
        char f1 = d[0], f2 = d[2];
        int n = d[4]-'0';
        int dist = friends.find(f1) - friends.find(f2);
        if (!check(d[3], abs(dist)-1, n)) return false;
    }
    return true;
}

void dfs(vector<string> data, int cnt) {
    if (cnt == FRIEND) {
        string sel = "";
        for (char s: selected) sel += s;
        if (isValid(data, sel)) answer++;
        return;
    }

    for (int i=0; i < FRIEND; i++) {
        if (visited[i]) continue;
        visited[i]=true;
        selected.push_back(friends[i]);
        dfs(data, cnt+1);
        selected.pop_back();
        visited[i]=false;
    }
}

int solution(int n, vector<string> data) {
    answer = 0;
    friends = "ACFJMNRT";
    dfs(data, 0);
    return answer;
}

 

DFS๋กœ ์ˆœ์—ด์„ ์กฐํšŒํ•˜๋ฉฐ ์ฒดํฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๊ทผ๋ฐ ์„ฑ๋Šฅ์€ next_permutation์ด  ๋„˜์‚ฌ๋‹ค

 

ํ†ต๊ณผ (9266.66ms, 4.32MB)

 

 

 

 

 

 

 

'ETC > Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

programmers, ๋‹จ์†์นด๋ฉ”๋ผ  (0) 2021.12.03
Programmers, ์„ฌ ์—ฐ๊ฒฐํ•˜๊ธฐ  (2) 2021.12.02
Programmers, ์ง€ํ˜• ํŽธ์ง‘  (0) 2021.11.30
Programmers, ๊ธฐ์ง€๊ตญ ์„ค์น˜  (0) 2021.11.29
Programmers, ๋ฐฐ๋‹ฌ  (0) 2021.11.29