-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation.java
More file actions
49 lines (41 loc) · 1.24 KB
/
Permutation.java
File metadata and controls
49 lines (41 loc) · 1.24 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
46
47
48
49
package org.study;
import java.util.*;
/*
* 순열 : n 개 중에서 r개 선택
* 시간복잡도는 O(n!)
* 연습 문제 : https://www.acmicpc.net/problem/10974
*/
public class Permutation {
public static void main(String[] args) {
int[] arr = {1,2,3};
int[] output = new int[arr.length];
boolean[] visited = new boolean[arr.length];
perm(arr, output, visited, 0, arr.length, 3);
}
// 순서를 지키면서 n개중 r개를 뽑는 경우
// 사용 예시 : perm(arr, output, visited, 0, n, 3);
static void perm(int[] arr, int[] output, boolean[] visited, int depth, int n, int r) {
//- 현재 깊이는 뽑은 개수이다
//- 즉, 뽑은 개수와 뽑을 개수가 같다면 종료.
if(depth == r) {
print(output,r);
return;
}
// 1. 모든 원소를 순회한다.
for (int i=0; i<n; i++) {
// 2. 방문하지 않은 원소를 찾는다
if(visited[i] != true) {
visited[i] = true; // 방문표시를 한다
output[depth] = arr[i]; // 결과값 배열의 해당깊이 위치에 방문하지 않은 원소 값 대입
perm(arr, output, visited, depth+1, n, r);
visited[i] = false;
}
}
}
// 배열 출력
static void print(int[] arr, int r) {
for(int i=0; i<r; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
}