-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use strict"; | ||
|
||
/** | ||
* [백트래킹 문제] | ||
* - 중복을 허용하므로 visited는 필요없고, 반복문은 1부터 계속 돌아야한다. 그래야 1 1, 2 2, 3 3, 4 4가 나올 수 있다. | ||
*/ | ||
|
||
const [N, M] = require("fs") | ||
.readFileSync("/dev/stdin") | ||
.toString() | ||
.trim() | ||
.split(" ") | ||
.map((v) => +v); | ||
|
||
const solution = (N, M) => { | ||
let answer = []; | ||
// const visited = Array.from({ length: N + 1 }, () => false); | ||
|
||
const dfs = (i, arr) => { | ||
if (arr.length === M) { | ||
answer.push(arr.join(" ")); | ||
return; | ||
} else { | ||
for (let k = 1; k <= N; k++) { | ||
arr.push(k); | ||
// visited[k] = true; | ||
dfs(k + 1, arr); | ||
// visited[k] = false; | ||
arr.pop(); | ||
} | ||
} | ||
}; | ||
dfs(1, []); | ||
|
||
return answer.join("\n"); | ||
}; | ||
|
||
console.log(solution(N, M)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters