본문 바로가기
Algorithm & SQL/BAEKJOON

백준 JAVA 15649 N과 M(1)

by YoonJong 2022. 7. 26.
728x90

package BAEKJOON.Silver.Ⅲ;

import java.util.Scanner;

public class NO15649_4 {
    // 1부터 N까지 자연수 중에서 중복 없이 M개를 고른 수열
    //첫째 줄에 자연수 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 8)
    static int [] arr;
    static boolean [] visit;

    public static void dfs(int n , int m , int depth) {
        if( depth == m ) {
            for (int i = 0; i < m; i++) {
                System.out.print(arr[i] + " ");
            }
            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;
            }
        }
    }


    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);

    }
}
728x90

'Algorithm & SQL > BAEKJOON' 카테고리의 다른 글

백준 JAVA 15650 N과 M(2)  (0) 2022.07.27
백준 JAVA 15651 N과 M(3)  (0) 2022.07.26
백준 JAVA 11866 요세푸스 문제0  (0) 2022.07.21
백준 JAVA 1026 보물  (0) 2022.07.19
백준 JAVA 9012 괄호  (0) 2022.07.19

댓글