Skip to content

Commit

Permalink
Section7-6. 부분집합 구하기(DFS)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuseon25 committed Jan 28, 2025
1 parent e331815 commit 2ce364a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Section07/부분집합_DFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package 인프런.Section07;

import java.util.Scanner;

public class 부분집합_DFS {
static int n;
static int[] ch;

public void DFS(int L) {
if(L == n+1) {
String tmp = "";
for(int i = 1; i <= n; i++) {
if(ch[i] == 1) tmp += (i + " ");
}
if (tmp.length() > 0) {
System.out.println(tmp);
}
} else {
ch[L] = 1; //부분집합에 포함하는 경우
DFS(L+1);
ch[L] = 0; //부분집합에 포함하지 않는 경우
DFS(L+1);
}
}

public static void main(String[] args) {
부분집합_DFS t = new 부분집합_DFS();

Scanner scanner = new Scanner(System.in);

n = scanner.nextInt();
ch = new int[n+1];

t.DFS(1);
}
}

0 comments on commit 2ce364a

Please sign in to comment.