์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[์ธํ”„๋Ÿฐ ์„น์…˜8] ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ (Javascript)

์€์ง„ 2021. 6. 30. 11:44

์ธํ”„๋Ÿฐ

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[์ธํ”„๋Ÿฐ ์„น์…˜8] ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ (Javascript)
์œ ๋ฃŒ ๊ฐ•์˜์ธ ๊ด€๊ณ„๋กœ ๋ฌธ์ œ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.


โœ๏ธIdea Sketch

2021-06-30

1. M๊ฐœ๋ฅผ ๋ฝ‘๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ

2. DFS ์ข…๋ฃŒ์กฐ๊ฑด

  • DFS ๋งค๊ฐœ๋ณ€์ˆ˜ v : ๊ตฌ์Šฌ ๋ฝ‘์€ ํšŸ์ˆ˜
  • v >= m ์ด๋ฉด return

3. DFS ๋‚ด๋ถ€๋กœ์ง

  • let res = []
  • v๋Š” ๋ฝ‘๋Š” ํšŸ์ˆ˜ ์ด๋ฏ€๋กœ ๋ฝ‘๋Š” ์ˆซ์ž๋Š” for ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ
  • [3, 4], [4, 3] ๊ฐ™์ด ์ค‘๋ณตx
  • ์ด์ „์— ๋ฝ‘์€ ์ˆซ์ž์ •๋ณด ํ•„์š” --> DFS ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜ u
for (let i = u + 1; i <= n; i++) {
  res.push(i);
  DFS(v + 1, i);
  res.pop();
}


โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-06-30

let res = [];
let n = 4, m = 2;
let cnt = 0;

function DFS(v, u) {
  if (v >= m) {
    console.log(res.join(' '));
    cnt++;
    return;
  }

  for (let i = u + 1; i <= n; i++) {
    res.push(i);
    DFS(v + 1, i);
    res.pop();
  }
}

DFS(0, 0);
console.log(cnt);