Skip to content

Instantly share code, notes, and snippets.

@dalcon10028
Created January 1, 2020 08:54
Show Gist options
  • Select an option

  • Save dalcon10028/4eea4cb3662d4c2ccb02ba63ab286b62 to your computer and use it in GitHub Desktop.

Select an option

Save dalcon10028/4eea4cb3662d4c2ccb02ba63ab286b62 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Main {
static int N, M;
static int []num;
static boolean []visited;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
sc.close();
num = new int[N+1];
visited = new boolean[N+1];
bt(0);
System.out.print(sb);
}
static void bt(int count){
if (count == M) {
for (int i=0; i<M; i++)
sb.append(num[i]+" ");
sb.append("\n");
return;
}
for (int i=1; i<=N; i++){
if(!visited[i])
{
visited[i] = true;
num[count] = i;
bt(count+1);
visited[i] = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment