-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsetDfs.java
More file actions
34 lines (30 loc) · 790 Bytes
/
Copy pathSubsetDfs.java
File metadata and controls
34 lines (30 loc) · 790 Bytes
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
import java.util.Scanner;
public class SubsetDfs {
static int n;
static int[] ch;
public void DFS(int l) {
if (l == n + 1) {
StringBuilder tmp = new StringBuilder();
for (int i = 1; i <= n; i++) {
if (ch[i] != 0) {
tmp.append(i).append(" ");
}
}
if (tmp.length() > 0) {
System.out.println(tmp.toString());
}
return;
}
ch[l] = 1;
DFS(l+1);
ch[l] = 0;
DFS(l+1);
}
public static void main(String[] args) {
SubsetDfs t = new SubsetDfs();
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
ch = new int[n + 1];
t.DFS(1);
}
}