Skip to content

Instantly share code, notes, and snippets.

@beng9re
Created January 26, 2022 15:18
Show Gist options
  • Save beng9re/24548687f4124f3259eac14e545161ea to your computer and use it in GitHub Desktop.
Save beng9re/24548687f4124f3259eac14e545161ea to your computer and use it in GitHub Desktop.
순열과 조합 (백트래킹)
public static void permutation(int idx) {
if(idx == R) {
return;
}
for(int i = 0; i < list.length; ++i) {
if(visited[i]) continue;
resulList[idx] = list[i];
visited[i] = true;
permutation(idx+1);
visited[i] = false;
}
}
// 백트래킹 사용
// 사용 예시 : combination(arr, visited, 0, n, r)
static void combination(int[] arr, boolean[] visited, int start, int n, int r) {
if (r == 0) {
print(arr, visited, n);
return;
}
for (int i = start; i < n; i++) {
visited[i] = true;
combination(arr, visited, i + 1, n, r - 1);
visited[i] = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment