1
1
package backjoon ;
2
-
2
+ // https://www.acmicpc.net/problem/15649
3
+ // N과 M (1)
3
4
import java .io .BufferedReader ;
4
5
import java .io .IOException ;
5
6
import java .io .InputStreamReader ;
@@ -12,9 +13,9 @@ public class _15649 {
12
13
13
14
public static void main (String [] args ) throws IOException {
14
15
BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
16
+ // memory 19912 runtime 156
15
17
StringTokenizer st = new StringTokenizer (br .readLine ());
16
- // memory runtime
17
- int N = Integer .parseInt (br .readLine ());
18
+ int N = Integer .parseInt (st .nextToken ());
18
19
int M = Integer .parseInt (st .nextToken ());
19
20
20
21
arr = new int [M ];
@@ -24,6 +25,7 @@ public static void main(String[] args) throws IOException {
24
25
}
25
26
26
27
public static void dfs (int N , int M , int depth ) {
28
+ // arr배열의 길이(depth)가 M과 같다면 출력
27
29
if (depth == M ) {
28
30
for (int val : arr ) {
29
31
sb .append (val ).append (' ' );
@@ -32,13 +34,32 @@ public static void dfs(int N, int M, int depth) {
32
34
return ;
33
35
}
34
36
37
+ // 중복값 체크를 위한 visit배열
35
38
for (int i = 0 ; i < N ; i ++) {
36
- if (!visit [i ]) {
39
+ if (!visit [i ]) {// 사용여부판단
37
40
visit [i ] = true ;
38
41
arr [depth ] = i + 1 ;
39
- dfs (N , M , depth + 1 );
40
- visit [i ] = false ;
42
+ dfs (N , M , depth + 1 );// 재귀호출
43
+ visit [i ] = false ;//하나의 재귀함수가 끝나면 false로 바꿔줘야 다시 i라는 숫자 사용가능
41
44
}
42
45
}
43
46
}
44
47
}
48
+ /*
49
+ INPUT
50
+ 4 2
51
+
52
+ OUTPUT
53
+ 1 2
54
+ 1 3
55
+ 1 4
56
+ 2 1
57
+ 2 3
58
+ 2 4
59
+ 3 1
60
+ 3 2
61
+ 3 4
62
+ 4 1
63
+ 4 2
64
+ 4 3
65
+ */
0 commit comments