From e5dd341fab18a704af2fa836da5336cbe808eb9b Mon Sep 17 00:00:00 2001 From: lolicon0930 <14966910+llc0930@users.noreply.github.com> Date: Wed, 19 Jun 2024 19:53:20 +0800 Subject: [PATCH 1/4] Dealing with CRLF issues --- calc.js | 466 ++++++++++++++++++++++++++--------------------------- index.html | 208 ++++++++++++------------ 2 files changed, 337 insertions(+), 337 deletions(-) diff --git a/calc.js b/calc.js index dee668f..d03c408 100644 --- a/calc.js +++ b/calc.js @@ -1,233 +1,233 @@ -const blocks = [ - [ - [ - [1, 1], - [1, 1] - ] - ], - [ - [ - [2, 2, 2, 2] - ], - [ - [2], - [2], - [2], - [2] - ] - ], - [ - [ - [3, 3, 0], - [0, 3, 3] - ], - [ - [0, 3], - [3, 3], - [3, 0] - ] - ], - [ - [ - [0, 4, 4], - [4, 4, 0] - ], - [ - [4, 0], - [4, 4], - [0, 4] - ] - ], - [ - [ - [5, 0, 0], - [5, 5, 5] - ], - [ - [5, 5], - [5, 0], - [5, 0] - ], - [ - [5, 5, 5], - [0, 0, 5] - ], - [ - [0, 5], - [0, 5], - [5, 5] - ] - ], - [ - [ - [0, 0, 6], - [6, 6, 6] - ], - [ - [6, 6], - [0, 6], - [0, 6] - ], - [ - [6, 6, 6], - [6, 0, 0] - ], - [ - [6, 0], - [6, 0], - [6, 6] - ] - ], - [ - [ - [0, 7, 0], - [7, 7, 7] - ], - [ - [7, 7, 7], - [0, 7, 0] - ], - [ - [7, 0], - [7, 7], - [7, 0] - ], - [ - [0, 7], - [7, 7], - [0, 7] - ] - ], - [ - [ - [0, 8, 0], - [8, 8, 8], - [0, 8, 0] - ] - ], - [ - [ - [9] - ] - ], - [ - [ - [10, 10] - ], - [ - [10], - [10] - ] - ], - [ - [ - [11, 11], - [11, 0], - ], - [ - [11, 11], - [0, 11], - ], - [ - [0, 11], - [11, 11], - ], - [ - [11, 0], - [11, 11], - ], - ] -]; - -let m, n, a, l, res; - -function Solve(arr, num) { - res = []; - m = arr.length; - n = arr[0].length; - - a = new Array(m); - for (let i = 0; i < m; ++i) { - a[i] = arr[i].map(x => x); - } - - l = num.map(x => x); - - dfs(0); - - // Sort res by the number of distinct elements in each matrix, in descending order - // If the number of distinct elements is the same, sort by the highest number in l for each element used in A and B - res.sort((A, B) => { - let distinctA = new Set(A.flat()).size; - let distinctB = new Set(B.flat()).size; - - if (distinctA !== distinctB) { - return distinctB - distinctA; - } else { - let maxA = Math.max(...A.flat().map(x => l[x-1])); - let maxB = Math.max(...B.flat().map(x => l[x-1])); - return maxB - maxA; - } - }); - - return res; -} - -function canPlaceBlock(x, y, b, d) { - const pat = blocks[b][d]; - let offset = 0; - while (!pat[0][offset]) ++offset; - y -= offset; - if (y < 0) return false; - for (let i = 0; i < pat.length; ++i) { - for (let j = 0; j < pat[0].length; ++j) { - if (pat[i][j] && (x + i >= m || y + j >= n || a[x + i][y + j] !== -1)) return false; - } - } - return true; -} - -function placeBlock(x, y, b, d, v) { - const pat = blocks[b][d]; - let offset = 0; - while (!pat[0][offset]) ++offset; - y -= offset; - for (let i = 0; i < pat.length; ++i) { - for (let j = 0; j < pat[0].length; ++j) { - if (pat[i][j]) a[x + i][y + j] = v; - } - } -} - -function dfs(p) { - if (p === m * n) { - const x = new Array(m); - for (let i = 0; i < m; ++i) { - x[i] = a[i].map(x => x); - } - res.push(x); - if (res.length >= 10000) { - alert('方案数太多,仅计算前一万种。减少一些方块吧~'); - return true; - } - return false; - } - const x = Math.floor(p / n), y = p % n; - if (a[x][y] !== -1) { - if (dfs(p + 1)) return true; - return false; - } - for (let b = 0; b < blocks.length; ++b) { - if (!l[b]) continue; - for (let d = 0; d < blocks[b].length; ++d) { - if (!canPlaceBlock(x, y, b, d)) continue; - placeBlock(x, y, b, d, b + 1); - --l[b]; - if (dfs(p + 1)) return true; - ++l[b]; - placeBlock(x, y, b, d, -1); - } - } - return false; -} - +const blocks = [ + [ + [ + [1, 1], + [1, 1] + ] + ], + [ + [ + [2, 2, 2, 2] + ], + [ + [2], + [2], + [2], + [2] + ] + ], + [ + [ + [3, 3, 0], + [0, 3, 3] + ], + [ + [0, 3], + [3, 3], + [3, 0] + ] + ], + [ + [ + [0, 4, 4], + [4, 4, 0] + ], + [ + [4, 0], + [4, 4], + [0, 4] + ] + ], + [ + [ + [5, 0, 0], + [5, 5, 5] + ], + [ + [5, 5], + [5, 0], + [5, 0] + ], + [ + [5, 5, 5], + [0, 0, 5] + ], + [ + [0, 5], + [0, 5], + [5, 5] + ] + ], + [ + [ + [0, 0, 6], + [6, 6, 6] + ], + [ + [6, 6], + [0, 6], + [0, 6] + ], + [ + [6, 6, 6], + [6, 0, 0] + ], + [ + [6, 0], + [6, 0], + [6, 6] + ] + ], + [ + [ + [0, 7, 0], + [7, 7, 7] + ], + [ + [7, 7, 7], + [0, 7, 0] + ], + [ + [7, 0], + [7, 7], + [7, 0] + ], + [ + [0, 7], + [7, 7], + [0, 7] + ] + ], + [ + [ + [0, 8, 0], + [8, 8, 8], + [0, 8, 0] + ] + ], + [ + [ + [9] + ] + ], + [ + [ + [10, 10] + ], + [ + [10], + [10] + ] + ], + [ + [ + [11, 11], + [11, 0], + ], + [ + [11, 11], + [0, 11], + ], + [ + [0, 11], + [11, 11], + ], + [ + [11, 0], + [11, 11], + ], + ] +]; + +let m, n, a, l, res; + +function Solve(arr, num) { + res = []; + m = arr.length; + n = arr[0].length; + + a = new Array(m); + for (let i = 0; i < m; ++i) { + a[i] = arr[i].map(x => x); + } + + l = num.map(x => x); + + dfs(0); + + // Sort res by the number of distinct elements in each matrix, in descending order + // If the number of distinct elements is the same, sort by the highest number in l for each element used in A and B + res.sort((A, B) => { + let distinctA = new Set(A.flat()).size; + let distinctB = new Set(B.flat()).size; + + if (distinctA !== distinctB) { + return distinctB - distinctA; + } else { + let maxA = Math.max(...A.flat().map(x => l[x-1])); + let maxB = Math.max(...B.flat().map(x => l[x-1])); + return maxB - maxA; + } + }); + + return res; +} + +function canPlaceBlock(x, y, b, d) { + const pat = blocks[b][d]; + let offset = 0; + while (!pat[0][offset]) ++offset; + y -= offset; + if (y < 0) return false; + for (let i = 0; i < pat.length; ++i) { + for (let j = 0; j < pat[0].length; ++j) { + if (pat[i][j] && (x + i >= m || y + j >= n || a[x + i][y + j] !== -1)) return false; + } + } + return true; +} + +function placeBlock(x, y, b, d, v) { + const pat = blocks[b][d]; + let offset = 0; + while (!pat[0][offset]) ++offset; + y -= offset; + for (let i = 0; i < pat.length; ++i) { + for (let j = 0; j < pat[0].length; ++j) { + if (pat[i][j]) a[x + i][y + j] = v; + } + } +} + +function dfs(p) { + if (p === m * n) { + const x = new Array(m); + for (let i = 0; i < m; ++i) { + x[i] = a[i].map(x => x); + } + res.push(x); + if (res.length >= 10000) { + alert('方案数太多,仅计算前一万种。减少一些方块吧~'); + return true; + } + return false; + } + const x = Math.floor(p / n), y = p % n; + if (a[x][y] !== -1) { + if (dfs(p + 1)) return true; + return false; + } + for (let b = 0; b < blocks.length; ++b) { + if (!l[b]) continue; + for (let d = 0; d < blocks[b].length; ++d) { + if (!canPlaceBlock(x, y, b, d)) continue; + placeBlock(x, y, b, d, b + 1); + --l[b]; + if (dfs(p + 1)) return true; + ++l[b]; + placeBlock(x, y, b, d, -1); + } + } + return false; +} + diff --git a/index.html b/index.html index 293ec8d..5482019 100644 --- a/index.html +++ b/index.html @@ -1,105 +1,105 @@ - - - -
- - -行数:
-列数:
- -红色表示需要摆放的格子,白色表示这里没有格子,点击切换
-方块{{ i + 1 }}个数:
- -方案数:{{ res.length }}
-当前展示方案:{{ now + 1 }} / {{ res.length }}
- 0)"> - -行数:
+列数:
+ +红色表示需要摆放的格子,白色表示这里没有格子,点击切换
+方块{{ i + 1 }}个数:
+ +方案数:{{ res.length }}
+当前展示方案:{{ now + 1 }} / {{ res.length }}
+ 0)"> + +