백준

백준 15649번 문제 N과 M (1) (Java)

gxxgsta 2021. 7. 14. 22:14
반응형
SMALL
import java.util.Scanner;

public class Main{
	public static int[] arr;
	public static boolean[] visit;
	
    public static void main(String args[]){
    	Scanner sc = new Scanner(System.in);
    	int N = sc.nextInt();
    	int M = sc.nextInt();
    	
    	arr = new int[M];
    	visit = new boolean[N];
    	dfs(N, M, 0);
    	
    	
    }
    public static void dfs(int N, int M, int depth){
    	if(depth ==  M) {
    		for(int num : arr) {
    			System.out.print(num+" ");
    		}
    		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;
    		}
    	}
    }
}

백트래킹 중 dfs를 사용

반응형
LIST