Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.05.30 - [BOJ] 2529. 부등호 #76

Closed
suhyunsim opened this issue May 30, 2021 · 0 comments
Closed

21.05.30 - [BOJ] 2529. 부등호 #76

suhyunsim opened this issue May 30, 2021 · 0 comments
Assignees
Labels

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented May 30, 2021

문제

핵심 아이디어

  • 숫자 10개를 10개의 위치에 넣는 것 -> 10! 모든 경우 해봐도 된다
  • 재귀로 풀이
    • 수의 인덱스, 부등호의 인덱스 (i번째 수와 i + 1번째 수의 비교는 i번째 인덱스로 비교하기)
  • 백트래킹으로 풀이
    • 이미 중간에 부등호 틀리게 수를 넣는 경우 -> 이후 재귀가 의미 없음
    • 그런 경우가 존재할 때 함수 호출하지 않기
    • 사용하지 않고, 대소관계 지킬 때만 재귀를 호출하도록
    • 한 자리 숫자이기 때문에 문자로 크기 비교 해도 ok
    • 첫 번째 숫자는 비교할 필요 없음

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q2529;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    static int k;
    static char[] a = new char[20];
    static ArrayList<String> answer = new ArrayList<>();
    static boolean[] check = new boolean[10];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        k = sc.nextInt();
        for (int i = 0; i < k; i++) {
            a[i] = sc.next().toCharArray()[0];
        }
        go(0, "");
        Collections.sort(answer);
        System.out.println(answer.get(answer.size() - 1));
        System.out.println(answer.get(0));
    }

    private static void go(int index, String num) {
        if (index == k + 1) {
            answer.add(num);
            return;
        }
        for (int i = 0; i <= 9; i++) {
            if (check[i]) continue; //중복 방지
            if (index == 0 || good(num.charAt(index - 1), (char) (i + '0'), a[index - 1])) { //부등호 틀린 것 검증
                check[i] = true;
                go(index + 1, num + i);
                check[i] = false;
            }
        }
    }

    private static boolean good(char x, char y, char op) {
        if (op == '<') {
            if (x > y) return false;
        }
        if (op == '>') {
            if (x < y) return false;
        }
        return true;
    }
}
@suhyunsim suhyunsim added this to the 5월 4주 차 milestone May 30, 2021
@suhyunsim suhyunsim self-assigned this May 30, 2021
@suhyunsim suhyunsim added the 성공 맞은 문제 label Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant