回答編集履歴
1
追記
test
CHANGED
@@ -62,3 +62,52 @@
|
|
62
62
|
|
63
63
|
}).mount("#app")
|
64
64
|
```
|
65
|
+
|
66
|
+
# 追記
|
67
|
+
|
68
|
+
ご解決のようですが、せっかくなので愚直に再帰するコードを提示します。
|
69
|
+
```js
|
70
|
+
const shuffle = (src) =>
|
71
|
+
src.reduce((a, x) => {
|
72
|
+
const j = Math.floor(Math.random() * (a.length + 1));
|
73
|
+
[a[a.length], a[j]] = [a[j], x];
|
74
|
+
return a;
|
75
|
+
}, []);
|
76
|
+
const origNames = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
77
|
+
const num = 5;
|
78
|
+
const len = origNames.length / num;
|
79
|
+
const arr = Array.from(new Array(num), () => []);
|
80
|
+
|
81
|
+
const names = shuffle(origNames)
|
82
|
+
// ↓たぶん最悪
|
83
|
+
// const names = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15];
|
84
|
+
|
85
|
+
const setMember = (names, arr) => {
|
86
|
+
[names, arr] = [structuredClone(names), structuredClone(arr)];
|
87
|
+
const selection = names.shift();
|
88
|
+
for (const row of arr) {
|
89
|
+
// その日がいっぱいなら次の日
|
90
|
+
if (row.length == len) continue;
|
91
|
+
// 重複するなら次の日
|
92
|
+
if (row.includes(selection)) continue;
|
93
|
+
row.push(selection);
|
94
|
+
|
95
|
+
// 再帰終了
|
96
|
+
if (names.length === 0) return [names, arr];
|
97
|
+
|
98
|
+
// 再帰
|
99
|
+
const re = setMember(names, arr);
|
100
|
+
|
101
|
+
// 探索失敗なら別の枝を探して次の日
|
102
|
+
if (!re) {
|
103
|
+
row.pop();
|
104
|
+
continue;
|
105
|
+
}
|
106
|
+
return re;
|
107
|
+
}
|
108
|
+
// 全ての日に置けないなら探索失敗
|
109
|
+
return false;
|
110
|
+
}
|
111
|
+
|
112
|
+
console.log('re:', setMember(names, arr)[1])
|
113
|
+
```
|