Notice
Recent Posts
Recent Comments
Link
04-30 23:35
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- route 53
- springboot
- 리눅스
- 클라우드 서비스 개발
- M2
- dbeaver
- 클라우드 서비스 개발 #
- 글로벌소프트웨어캠퍼스
- 우리FIS아카데미
- 도메인
- AWS
- sts
- 맥북
- Java
- HTTP
- mysql
- 우리FISA #
- https
- Gradle
- 로드밸런스
- 우리FISA
- 우리에프아이에스 #
- 우리에프아이에스
- 우리FIS아카데미 #
- 맥OS
- jdk
- 맥
- K-디지털트레이닝
- spring
Archives
- Today
- Total
<<개발일지>>
[DFS] 6. 순열 구하기 본문
<<풀이>>
순열 : 서로 다른 n 개 중 r개를 골라 순서를 정해 나열하는 가짓수
흔히 우리는 이를 nPr 이렇게 해서 풀어 왔었다. 이를 코드로 하면 아래와 같은 코드로 되는데
이때 DFS를 이용해서 구하면 구현이 가능하다.
이전 부터 해오던 ch를 통해 중복되는 경우는 제외시켜준다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
class Main {
static int n, m;
static int[] brr, answer, ch;
private void DFS(int L) {
if (L == m) {
for(int x : answer) {
System.out.print(x + " ");
}
System.out.println();
} else {
for(int a : brr) {
if (ch[a] == 0) {
ch[a] = 1;
answer[L] = a;
DFS(L + 1);
ch[a] = 0;
}
}
}
}
public static void main(String[] args) throws Exception{
Main T = new Main();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] arr = br.readLine().split(" ");
n = Integer.parseInt(arr[0]);
m = Integer.parseInt(arr[1]);
brr = Arrays.asList(br.readLine().split(" "))
.stream()
.mapToInt(Integer::parseInt)
.toArray();
answer = new int[m];
ch = new int[100];
T.DFS(0);
}
}
'코딩테스트' 카테고리의 다른 글
[DFS] 7. 조합의 경우수(메모이제이션) (6) | 2024.02.26 |
---|---|
[백준/1002/실버3] 터렛 (3) | 2024.02.25 |
[DFS] 5. 동전교환 (6) | 2024.02.24 |
[백준/1051/실버3] 숫자 정사각형 (1) | 2024.02.22 |
[DFS] 3. 최대점수 구하기 (1) | 2024.02.22 |