-
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.
Browse files
Browse the repository at this point in the history
15649~15650 N과 M (1~2) 다시 풀기
- Loading branch information
Showing
3 changed files
with
49 additions
and
24 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 @@ | ||
4 4 |
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,27 @@ | ||
const solution = (N, M) => { | ||
const answer = []; | ||
|
||
const visited = Array(N + 1).fill(false); | ||
visited[0] = null; | ||
|
||
const dfs = (arr) => { | ||
if (arr.length === M) answer.push(arr); | ||
else { | ||
for (let i = 1; i <= N; i++) { | ||
if (!visited[i]) { | ||
visited[i] = true; | ||
dfs([...arr, i]); | ||
visited[i] = false; | ||
} | ||
} | ||
} | ||
}; | ||
dfs([]); | ||
return answer.map((arr) => arr.join(' ')).join('\n'); | ||
}; | ||
|
||
const fs = require('fs'); | ||
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt'; | ||
const [N, M] = fs.readFileSync(file).toString().trim().split(' ').map(Number); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,28 @@ | ||
const solution = (N, M) => { | ||
const getCombinations = (arr, selectNumber) => { | ||
const results = []; | ||
if (selectNumber === 1) return arr.map((el) => [el]); | ||
arr.forEach((fixed, index, origin) => { | ||
const rest = origin.slice(index + 1); | ||
const combinations = getCombinations(rest, selectNumber - 1); | ||
const attached = combinations.map((el) => [fixed, ...el]); | ||
results.push(...attached); | ||
}); | ||
return results; | ||
}; | ||
const answer = []; | ||
|
||
const visited = Array(N + 1).fill(false); | ||
visited[0] = null; | ||
|
||
getCombinations( | ||
Array(N) | ||
.fill(null) | ||
.map((_, idx) => idx + 1), | ||
M | ||
).forEach((c) => console.log(...c)); | ||
const dfs = (arr) => { | ||
if (arr.length === M) answer.push(arr); | ||
else { | ||
let i = arr.length ? arr[arr.length - 1] + 1 : 1; | ||
for (; i <= N; i++) { | ||
if (!visited[i]) { | ||
visited[i] = true; | ||
dfs([...arr, i]); | ||
visited[i] = false; | ||
} | ||
} | ||
} | ||
}; | ||
dfs([]); | ||
return answer.map((arr) => arr.join(' ')).join('\n'); | ||
}; | ||
|
||
const fs = require('fs'); | ||
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt'; | ||
const [N, M] = fs | ||
.readFileSync(file) | ||
.toString() | ||
.trim() | ||
.split(' ') | ||
.map((c) => +c); | ||
const [N, M] = fs.readFileSync(file).toString().trim().split(' ').map(Number); | ||
|
||
solution(N, M); | ||
console.log(solution(N, M)); |