We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 346675c commit ac2a0f4Copy full SHA for ac2a0f4
1 file changed
live9/test99/문제2/조진우.js
@@ -0,0 +1,38 @@
1
+const fs = require("fs");
2
+const input = fs
3
+ .readFileSync("./input.txt", "utf8")
4
+ // .readFileSync("/dev/stdin", "utf8")
5
+ .toString()
6
+ .trim()
7
+ .split("\n")
8
+ .map((line) => line.trim().split(" ").map(Number));
9
+
10
+function solution(input) {
11
+ const [N, M] = input[0];
12
+ const arr = input[1];
13
+ arr.sort((a, b) => a - b); // 사전 순 출력을 위해 정렬
14
15
+ const visited = Array(N).fill(false);
16
+ const path = [];
17
18
+ function dfs(depth) {
19
+ if (depth === M) {
20
+ console.log(path.join(" ")); // 바로 출력!
21
+ return;
22
+ }
23
24
+ for (let i = 0; i < N; i++) {
25
+ if (visited[i]) continue;
26
27
+ visited[i] = true;
28
+ path.push(arr[i]);
29
+ dfs(depth + 1);
30
+ path.pop();
31
+ visited[i] = false;
32
33
34
35
+ dfs(0); // 시작
36
+}
37
38
+solution(input);
0 commit comments