Skip to content

Commit

Permalink
#18 23.08.25 > 3 > N과 M(3)
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Aug 25, 2023
1 parent 4b165d8 commit c108cff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/bj/silver/3/15651.js
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));
1 change: 1 addition & 0 deletions src/bj/silver/3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@
| 41 | 14425 | [문자열 집합](./14425.js) | 23.06.04 | O |
| 42 | 16165 | [걸그룹 마스터 준석이](./16165.js) | 23.07.10 | O |
| 43 | 13414 | [수강신청](./13414.js) | 23.08.22 | O |
| 44 | 15651 | [N과 M(3)](./15651.js) | 23.08.25 | O |

0 comments on commit c108cff

Please sign in to comment.