Jun's Development Journey
[SWEA][S/W 문제해결 응용] Baby Gin (완전탐색 - 순열 이용) 본문
문제
3, 4, 5와 같이 연속된 세 수가 1 차이로 등차일 경우를 Run이라 칭하고, 7, 7, 7와 같이 같은 수가 세 번 연속된 수를 Triplet이라고 한다.
여섯개의 숫자 중, 3개는 Run, 3개는 Triplet을 이룰 때 Baby Gin이라고 한다. 여섯 개의 수가 주어졌을 때 Baby Gin임을 판별하는 알고리즘을 작성하시오.
풀이
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
|
public class Main {
//Baby Gin 문제
static int[] arr = {2,3,7,7,3,7};
static boolean chk = false;
//run 검사
static boolean is_run(int st, int end) {
return true;
return false;
}
//triplet 검사
static boolean is_triplet(int st) {
if(arr[st]==arr[st+1] && arr[st]==arr[st+2])
return true;
return false;
}
//교환 방식(순열 구하기)
static void swap(int idx1, int idx2) {
int tNum = arr[idx1];
arr[idx1] = arr[idx2];
arr[idx2] = tNum;
}
public static void dfs_swap(int depth, int size) {
if(depth == size && !chk) {
if((is_run(0,2) && is_triplet(3))||
(is_run(3,5) && is_triplet(0))) {
chk = true;
for(int i=0;i<6;i++)
System.out.print(arr[i]);
System.out.println();
}
}
else {
for(int i=depth;i<6;i++) {
swap(depth,i);
dfs_swap(depth+1,size);
swap(depth,i);
}
}
}
public static boolean is_babygin(int[] arr) {
dfs_swap(0,arr.length);
return chk;
}
public static void main(String[] args) {
System.out.println(is_babygin(arr));
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'SWEA > Advanced' 카테고리의 다른 글
[SWEA][S/W 문제해결 응용] 피보나치 수 (0) | 2019.09.26 |
---|