Created
January 1, 2020 08:54
-
-
Save dalcon10028/4eea4cb3662d4c2ccb02ba63ab286b62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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