https://www.acmicpc.net/problem/15652
백트래킹
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static StringBuilder sb = new StringBuilder();
static int arr[];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
arr = new int[M];
int depth = 0;
dfs(N, M, depth, 0);
System.out.println(sb);
}
public static void dfs(int N, int M, int depth, int start){
if (M == depth) {
for(int num : arr) {
sb.append(num).append(" ");
}
sb.append("\n");
return;
}
for(int i=start; i<N; i++){
arr[depth] = i+1;
start = i;
dfs(N, M , depth+1, start);
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 15651번 - N과 M (3) (Java 8) (0) | 2022.03.07 |
---|---|
백준 9663번 - N-Queen (Java 8) (0) | 2022.03.07 |
백준 15650번 - N과 M (2) - (Java 8) (0) | 2022.03.06 |
백준 15649번 - N과 M (1) (Java 8) (0) | 2022.03.05 |
백준 18870번 - 좌표 압축 (Java 8) (0) | 2022.03.05 |
댓글