2021년 10월 03일 일요일 15시
[백트래킹] 백준_15649_N과M(1) _백트래킹_실버3
https://www.acmicpc.net/problem/15649
import java.util.Scanner;
public class Main{
public static boolean[] visit;
public static int[] arr;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int M = in.nextInt();
visit = new boolean[N];
arr = new int[M];
dfs(N,M,0);
}
public static void dfs(int N, int M, int depth) {
if(depth == M) {
for(int val: arr) {
System.out.print(val + " ");
}
System.out.println();
return;
}
for(int i=0; i<N; i++) {
if( !visit[i]) {
visit[i] = true;
arr[depth] = i +1;
dfs(N,M, depth + 1);
visit[i] = false;
}
}
}
}
<참고>
https://st-lab.tistory.com/114
https://iseunghan.tistory.com/229
'알고리즘 > 백준' 카테고리의 다른 글
[DFSBFS] 백준_1260_DFSBFS_DFSBFS_실버2 (0) | 2021.10.14 |
---|---|
[백트래킹] 백준_9663_NQueen_백트래킹_골드5 (0) | 2021.10.03 |
[재귀] 백준_11729_하노이탑_재귀_실버2 (0) | 2021.10.03 |
[재귀] 백준_10870_피보나치수 5_재귀_브론즈2 (0) | 2021.10.02 |
[재귀] 백준_10872_팩토리얼_재귀_브론즈3 (0) | 2021.10.02 |