[백준] 2529번 : 부등호

김개발·2021년 9월 20일
0

백준

목록 보기
20/75

문제 푼 날짜 : 2021-09-20

문제

문제 링크 : https://www.acmicpc.net/problem/2529

접근 및 풀이

숫자를 하나하나 추가해갈 때마다 부등호가 만족하는지 체크해주었다.
숫자들이 전부 선택됐을 때, 그 값들을 vector에 저장해주었고, 최소,최대값은 minmax_element 함수를 이용하였다.

코드

// 백준 2529번 : 부등호
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int N;
char arr[10];
bool selected[10] = { false };

vector<string> v;

void dfs(int idx, int cnt, string candid) {
    if (cnt == N + 1) {
        v.push_back(candid);
        return;
    }
    for (int now = 0; now <= 9; now++) {
        if (selected[now]) {
            continue;
        }
        char ch_now = now + '0';
        int before = candid[idx] - '0';

        selected[now] = true;
        if (arr[idx] == '<' && before < now) {
            dfs(idx + 1, cnt + 1, candid + ch_now);
        } else if (arr[idx] == '>' && before > now) {
            dfs(idx + 1, cnt + 1, candid + ch_now);
        }
        selected[now] = false;
    }
}

int main() {
    cin >> N;

    for (int i = 0; i < N; i++) {
        cin >> arr[i];
    }
    for (int i = 0; i <= 9; i++) {
        string tmp = "";
        char ch = i + '0';
        tmp += ch;
        selected[i] = true;
        dfs(0, 1, tmp);
        selected[i] = false;
    }
    sort(v.begin(), v.end());
    auto ret = minmax_element(v.begin(), v.end());
    
    cout << *ret.second << '\n' << *ret.first;
    return 0;
}

결과

피드백

다른 분들의 풀이를 보니 내가 엄청 지저분하게 구현했다는 것을 알게됐다..
아직 많이 부족한 것 같다.ㅠㅠ

profile
개발을 잘하고 싶은 사람

0개의 댓글