본문 바로가기
JAVA

[백준 15664]

by Son 2022. 8. 11.

package brute_force;

import java.util.Arrays;
import java.util.Scanner;

public class N_15664 {

private static int n,m;
private static int[] map;
private static int[] result;
private static boolean[] visit;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

n = sc.nextInt();
m = sc.nextInt();

map = new int[n];
result = new int[m];
visit = new boolean[n+1];

for (int i=0; i<n; i++) {
map[i] = sc.nextInt();
}

Arrays.sort(map);

StringBuilder sb = new StringBuilder();

cycle(0, 0, sb);

System.out.print(sb);

}

private static void cycle(int start, int cnt, StringBuilder sb) {

if (cnt == m) {

for (int i=0; i<m; i++) {
sb.append(result[i] + " ");
}
sb.append("\n");

} else {

int num = 0;
for (int i=start; i<n; i++) {
if (!visit[i]) {
if (num == map[i]) {  //중복이라면 생략
continue;
}

visit[i] = true;
result[cnt] = map[i];
cycle(i+1, cnt+1, sb);
visit[i] = false;

num = map[i];  //같은 깊이에서 중복되면 깊이 탐색을 하지 않기 위함

}
}

}

}
}

'JAVA' 카테고리의 다른 글

백준 15666  (0) 2022.08.24
[백준 15665]  (0) 2022.08.16
[백준] 15663번  (0) 2022.08.08
[백준 15657]  (0) 2022.08.07
백준 15656번  (0) 2022.08.02