https://www.acmicpc.net/problem/1174
처음엔 0, 1, 2, 3, 4 이런식으로 탐색을 하면서 카운트를 해야하나 생각하고 너비탐색을 짜보려고 하다가.. 이런 방향이 맞는가 싶어서
풀이를 찾아보았고
그냥 깊이우선탐색 방식으로 완전탐색 해도 시간초과가 되지 않겠구나 생각이 들었다
배열에 숫자를 담을때 이전에 담은 숫자보다 현재 담을 숫자가 더 작은지 체크하고 담도록 하였다 숫자를 담을떼 최종 깊이 레벨이 10 미만이 되도록 하였다.
그리고 정렬까지 하였다
Integer 로 하니 마지막 1023번째 숫자를 담을 수 없어서 이상한 값으로 바뀌는 오류가 나는것을 모르고 한참 찾았다. Long 으로 해결하였다
import java.io.*;
import java.util.*;
public class Main
{
static BufferedReader br;
static int N;
static final int CNTNUM = 10;
static int[] arr = new int[CNTNUM];
static boolean[] checked = new boolean[CNTNUM];
static int count;
static ArrayList<Long> numList = new ArrayList<>();
static Queue<Integer> que = new LinkedList<>();
public static void main(String[] args) throws IOException
{
inputProcess();
search(0, 1);
numList.sort( (a, b) -> (a < b) ? -1 : 1);
if(N > 1023)
System.out.println(-1);
else
System.out.println(numList.get(N-1));
}
public static void inputProcess() throws IOException
{
br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
}
public static void search(int depth, int target)
{
if(target > 10)
return;
if(depth == target)
{
long temp = 0;
for(int i = 0; i < target; ++i)
{
temp *= 10;
temp += arr[i];
}
numList.add(temp);
return;
}
for(int i = 0; i < 10; ++i)
{
if(checked[i] == false && (depth==0 || arr[depth-1] > i))
{
checked[i] = true;
arr[depth] = i;
search(depth+1, target);
search(depth+1, target + 1);
checked[i] = false;
}
}
}
}