Skip to content

Commit a46e6e6

Browse files
author
Eric
committed
99차 2번 문제풀이
1 parent af473ff commit a46e6e6

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

live9/test99/문제2/황장현.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const input = require('fs')
2+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
3+
.toString()
4+
.trim()
5+
.split('\n')
6+
.map((el) => el.split(' ').map(Number));
7+
8+
function solution(input) {
9+
const [N, M] = input[0];
10+
const number = input[1].sort((a, b) => a - b);
11+
12+
const visited = Array(N).fill(false);
13+
const path = [];
14+
const result = [];
15+
16+
function dfs(depth) {
17+
if (depth === M) {
18+
result.push(path.join(' '));
19+
return;
20+
}
21+
22+
for (let i = 0; i < N; i++) {
23+
if (!visited[i]) {
24+
visited[i] = true;
25+
path.push(number[i]);
26+
dfs(depth + 1);
27+
path.pop();
28+
visited[i] = false;
29+
}
30+
}
31+
}
32+
33+
dfs(0);
34+
35+
return result.join('\n');
36+
}
37+
38+
console.log(solution(input));

0 commit comments

Comments
 (0)