-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ10974.java
More file actions
45 lines (35 loc) · 1.11 KB
/
Copy pathJ10974.java
File metadata and controls
45 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package TIL;
import java.io.*;
import java.util.*;
public class J10974 {
static int[] arr;
static StringBuilder builder = new StringBuilder();
public static void main(String[] args) throws IOException{
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(buffer.readLine());
arr = new int[n];
for(int i = 0; i < n; i ++){
arr[i] = i+1;
}
int[] value = new int[n];
boolean[] visited = new boolean[n];
DFS(value,0,n,visited);
System.out.println(builder.toString());
}
public static void DFS(int[] num, int start, int end, boolean[] visited){
if(start == end){
for(int i =0; i< end; i++)
builder.append(num[i] + " ");
builder.append("\n");
return;
}
for(int i = 0; i<end; i++) {
if(!visited[i]) {
visited[i] = true;
num[start] = arr[i];
DFS(num,start+1, end, visited);
visited[i] = false;
}
}
}
}