티스토리 뷰

728x90

https://www.acmicpc.net/problem/5568

 

5568번: 카드 놓기

예제 1의 경우 상근이는 11, 12, 21, 112, 121, 122, 212를 만들 수 있다.

www.acmicpc.net

 

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
 
public class Main {
 
    static int list[], arr[];
    static int N = 0, K = 0;
    static boolean visit[];
    static HashSet<Integer> set = new HashSet<>();
 
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        K = sc.nextInt();
        arr = new int[K];
        list = new int[N];
        visit = new boolean[N];
        
        for (int i = 0; i < N; i++) {
            list[i] = sc.nextInt();
        }
        sc.close();
 
        dfs(0);
 
        System.out.println(set.size());
    }
 
    private static void dfs(int depth) {
        if (depth == K) {
            String sum = "";
            for (int i = 0; i < arr.length; i += 1) {
                sum += arr[i];
            }
            set.add(Integer.parseInt(sum));
            return;
        }
 
        for (int i = 0; i < N; i++) {
            if (!visit[i]) {
                visit[i] = true;
                arr[depth] = list[i];
                dfs(depth + 1);
                visit[i] = false;
            }
        }
    }
}
cs

 

댓글