-
Notifications
You must be signed in to change notification settings - Fork 15
/
calc.min.js
1 lines (1 loc) · 1.67 KB
/
calc.min.js
1
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(e,t){res=[],m=e.length,n=e[0].length,a=new Array(m);for(let t=0;t<m;++t)a[t]=e[t].map((e=>e));return l=t.map((e=>e)),dfs(0),res.sort(((e,t)=>{let n=new Set(e.flat()).size,r=new Set(t.flat()).size;if(n!==r)return r-n;{let n=Math.max(...e.flat().map((e=>l[e-1])));return Math.max(...t.flat().map((e=>l[e-1])))-n}})),res}function canPlaceBlock(e,t,l,r){const o=blocks[l][r];let f=0;for(;!o[0][f];)++f;if((t-=f)<0)return!1;for(let l=0;l<o.length;++l)for(let r=0;r<o[0].length;++r)if(o[l][r]&&(e+l>=m||t+r>=n||-1!==a[e+l][t+r]))return!1;return!0}function placeBlock(e,t,l,n,r){const o=blocks[l][n];let f=0;for(;!o[0][f];)++f;t-=f;for(let l=0;l<o.length;++l)for(let n=0;n<o[0].length;++n)o[l][n]&&(a[e+l][t+n]=r)}function dfs(e){if(e===m*n){const e=new Array(m);for(let t=0;t<m;++t)e[t]=a[t].map((e=>e));return res.push(e),res.length>=1e4&&(alert("方案数太多,仅计算前一万种。减少一些方块吧~"),!0)}const t=Math.floor(e/n),r=e%n;if(-1!==a[t][r])return!!dfs(e+1);for(let n=0;n<blocks.length;++n)if(l[n])for(let o=0;o<blocks[n].length;++o)if(canPlaceBlock(t,r,n,o)){if(placeBlock(t,r,n,o,n+1),--l[n],dfs(e+1))return!0;++l[n],placeBlock(t,r,n,o,-1)}return!1}