본문 바로가기
알고리즘/완전탐색

[java 백준] 골드 5/1759번 암호 만들기

by Meaning_ 2022. 3. 17.
728x90
반응형

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

 

1759번: 암호 만들기

첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
    public static int l, c;
    public static char[] arr;
    public static int vowels;
    public static int consonants;
 
    public static boolean visited[];
    public static char[] input;
 
    public static void DFS(int depth, int vowels, int consonants) {
 
        if (depth == l) {
            if (vowels >= 1 && consonants >= 2) {
                for (int i = 0; i < l; i++) {
                    System.out.print(arr[i]);
                }
                System.out.println();
            }
 
            return;
 
        } else {
            for (int i = 0; i < c; i++) {
 
                if (!visited[i]) {
                    if (depth == 0) {
 
                        visited[i] = true;
                        arr[0= input[i];
 
                        if (depth + 1 <= l) {
                            if (arr[depth] == 'u' || arr[depth] == 'e' || arr[depth] == 'i' || arr[depth] == 'o'
                                    || arr[depth] == 'a') {
                                DFS(depth + 1, vowels + 1, consonants);
 
                            } else {
                                DFS(depth + 1, vowels, consonants + 1);
                            }
                        }
                        visited[i] = false;
                    }
 
                    else if (depth > 0) {
 
                        if (input[i] > arr[depth - 1]) {
 
                            visited[i] = true;
 
                            arr[depth] = input[i];
 
                            if (arr[depth] == 'a' || arr[depth] == 'e' || arr[depth] == 'i' || arr[depth] == 'o'
                                    || arr[depth] == 'u') {
 
                                DFS(depth + 1, vowels + 1, consonants);
                            } else {
                                DFS(depth + 1, vowels, consonants + 1);
                            }
 
                            visited[i] = false;
                        }
 
                    }
 
                }
            }
 
        }
 
    }
 
    public static void main(String[] args) throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
 
        l = sc.nextInt();
 
        c = sc.nextInt();
        arr = new char[l];
        input = new char[c];
        visited = new boolean[c];
 
        for (int i = 0; i < c; i++) {
            String s = sc.next();// 공백 기준
            input[i] = s.charAt(0);
        }
        Arrays.sort(input);
        // 문자열 정렬
 
        vowels = 0;
        consonants = 0;
 
        DFS(0, vowels, consonants);
 
    }
 
}
cs

 

 

수 많은 런타임에러 끝에 드디어 풀어냈다. 지금 새벽 3시인데 발뻗고 잘 수 있을 것 같다..ㅎ 녹강은 다 밀림 ㅋㅋ ㅜㅜ

아무튼 왜 그렇게 많이 런타임에러를 뿜어냈을까 하다 찾아낸게

 

이 부분을 if(depth==1 && vowels==1 && consonants ==1)로 해버려서 그랬다..

 

아 그리고 반례는 l이 3일때로 하면 좋다. 만약 나처럼 코드를 짜면 l이 3일때 계속 런타임에러를 맛보게 될것이다 ㅋㅋㅋ!

728x90
반응형

댓글