Problem Solving/BOJ

[Silver 3] 15649번 N과 M (1)

kmkunk 2022. 4. 1. 14:37
import java.io.*;

public class Main {
    public static boolean[] tf;
    public static int[] ans;
    public static StringBuilder sb;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        sb = new StringBuilder();
        String[] arr = br.readLine().split(" ");
        int n = Integer.parseInt(arr[0]); int m = Integer.parseInt(arr[1]);
        tf = new boolean[n];
        ans = new int[m];
        
        calc(0, n, m);
        System.out.println(sb);
    }
    
    public static void calc(int index, int n, int m) {
        if(index==m) {
            for(int a : ans) { sb.append(String.valueOf(a)).append(" "); }
            sb.append("\n");
            return;
        }
        
        for(int i=0; i<n; i++) {
            if(tf[i]) { continue; }
            
            tf[i] = true;
            ans[index] = i+1;
            calc(index+1, n, m);
            tf[i] = false;
        }
    }
}