인프런 커뮤니티 질문&답변
pm배열
작성
·
196
0
pm배열 없이 ch를 이용해서 바로 답을 출력하도록 했는데 제 코드는 왜 오답인지 잘 모르겠습니다.
public class Ch8_6 {
public static int n, m;
public static int[] arr,ch;
public static void DFS(int length) {
if(length == m) {
for(int i = 0; i < n; i++) {
if(ch[i] == 1) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
} else {
for(int i = 0; i < n; i++) {
if(ch[i] == 0) {
ch[i] = 1;
DFS(length + 1);
ch[i] = 0;
}
}
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
arr = new int[n+1];
ch = new int[n+1];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
DFS(0);
}
}
퀴즈
71%나 틀려요. 한번 도전해보세요!
DFS를 활용한 부분집합 문제 해결의 핵심 아이디어는 무엇일까요?
힙(Heap) 자료구조를 이용해 우선순위 결정
각 원소를 포함하거나 포함하지 않는 두 가지 경로로 분기
너비 우선 탐색으로 모든 경우의 수 동시 탐색
이분 탐색으로 해(Solution)의 존재 여부 확인





