728x90
반응형
https://www.acmicpc.net/problem/1759
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
반응형
'알고리즘 > 완전탐색' 카테고리의 다른 글
[java 백준] 골드 4/ 1987번 알파벳 (0) | 2022.03.25 |
---|---|
[java 백준]골드 5/5014번 스타트링크 (0) | 2022.03.21 |
[java 백준] 실버 2 / 1182번 부분수열의 합 (0) | 2022.03.06 |
[java 백준] 실버 1/2251번 물통 (0) | 2022.02.27 |
[java 백준]골드 4/ 1963번 소수경로 (0) | 2022.02.26 |
댓글