Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

csct3434

[level 2] 단체사진 찍기 - 1835 본문

프로그래머스

[level 2] 단체사진 찍기 - 1835

csct3434 2024. 2. 28. 19:39
#include <bits/stdc++.h> 

using namespace std;

bool checkCondition(string friends, string cond) {
    char op = cond[3];
    int requiredDistance = cond[4] - '0';
        
    int distance = abs(int(friends.find(cond[0]) - friends.find(cond[2]))) - 1;
        
    if(op == '=') {
        if(distance != requiredDistance) {
            return false;
        }
    } else if(op == '<') {
        if(!(distance < requiredDistance)) {
            return false;
        }
    } else {
        if(!(distance > requiredDistance)) {
            return false;
        }
    }
    return true;
}

bool isPossible(string friends, vector<string> data) {
    for(auto cond : data) {
        if(!checkCondition(friends, cond)) {
            return false;
        }
    }
    return true;
}

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

    do {
        if(isPossible(friends, data)) {
            answer++;
        }
    } while(next_permutation(friends.begin(), friends.end()));
    
    return answer;
}