elo-worldle/gchessboard.min.js

2 lines
68 KiB
JavaScript
Raw Normal View History

2023-04-01 23:11:32 -04:00
const STAR=1;const PRIME=2;class Munkres{constructor(costMatrix,padValue){Object.defineProperty(this,"C",{enumerable:true,configurable:true,writable:true,value:void 0});Object.defineProperty(this,"n",{enumerable:true,configurable:true,writable:true,value:void 0});Object.defineProperty(this,"originalRows",{enumerable:true,configurable:true,writable:true,value:void 0});Object.defineProperty(this,"originalCols",{enumerable:true,configurable:true,writable:true,value:void 0});Object.defineProperty(this,"marked",{enumerable:true,configurable:true,writable:true,value:void 0});Object.defineProperty(this,"rowCovered",{enumerable:true,configurable:true,writable:true,value:void 0});Object.defineProperty(this,"colCovered",{enumerable:true,configurable:true,writable:true,value:void 0});Object.defineProperty(this,"Z0Row",{enumerable:true,configurable:true,writable:true,value:0});Object.defineProperty(this,"Z0Col",{enumerable:true,configurable:true,writable:true,value:0});const maxNumColumns=costMatrix.reduce((acc,row)=>Math.max(acc,row.length),0);this.n=Math.max(costMatrix.length,maxNumColumns);this.originalRows=costMatrix.length;this.originalCols=maxNumColumns;this.C=[];for(let i=0;i<this.n;i++){const row=costMatrix[i]===undefined?[]:costMatrix[i].slice();while(row.length<this.n){row.push(padValue||0)}this.C.push(row)}this.marked=this._makeMatrix(this.n,0);this.rowCovered=Array(this.n).fill(false);this.colCovered=Array(this.n).fill(false)}compute(){let step=1;const steps={1:this._step1,2:this._step2,3:this._step3,4:this._step4,5:this._step5,6:this._step6};while(step<7){const func=steps[step];step=func.apply(this)}const results=[];for(let i=0;i<this.originalRows;i++){for(let j=0;j<this.originalCols;j++){if(this.marked[i][j]==STAR){results.push([i,j])}}}return results}_makeMatrix(n,val){const matrix=[];for(let i=0;i<n;i++){const row=[];for(let j=0;j<n;j++){row.push(val)}matrix.push(row)}return matrix}_step1(){for(let i=0;i<this.n;i++){const minval=Math.min(...this.C[i]);for(let j=0;j<this.n;j++){this.C[i][j]-=minval}}return 2}_step2(){for(let i=0;i<this.n;i++){for(let j=0;j<this.n;j++){if(this.C[i][j]===0&&!this.rowCovered[i]&&!this.colCovered[j]){this.marked[i][j]=STAR;this.rowCovered[i]=true;this.colCovered[j]=true;break}}}this._clearCovers();return 3}_step3(){let count=0;for(let i=0;i<this.n;i++){for(let j=0;j<this.n;j++){if(this.marked[i][j]===STAR&&!this.colCovered[j]){this.colCovered[j]=true;count++}}}return count>=this.n?7:4}_step4(){let colWithStar=-1;for(;;){const[row,col]=this._findFirstUncoveredZero();if(row<0){return 6}this.marked[row][col]=PRIME;colWithStar=this._findStarInRow(row);if(colWithStar>=0){this.rowCovered[row]=true;this.colCovered[colWithStar]=false}else{this.Z0Row=row;this.Z0Col=col;return 5}}}_step5(){let count=0;const path=[[this.Z0Row,this.Z0Col]];for(;;){const row=this._findStarInCol(path[count][1]);if(row<0){break}path.push([row,path[count][1]]);count++;const col=this._findPrimeInRow(path[count][0]);path.push([path[count][0],col]);count++}for(let i=0;i<=count;i++){const[row,col]=path[i];this.marked[row][col]=this.marked[row][col]==STAR?0:STAR}this._clearCovers();this._erasePrimes();return 3}_step6(){const minval=this._findSmallestUncovered();for(let i=0;i<this.n;i++){for(let j=0;j<this.n;j++){if(this.rowCovered[i]){this.C[i][j]+=minval}if(!this.colCovered[j]){this.C[i][j]-=minval}}}return 4}_clearCovers(){for(let i=0;i<this.n;i++){this.rowCovered[i]=false;this.colCovered[i]=false}}_erasePrimes(){for(let i=0;i<this.n;i++){for(let j=0;j<this.n;j++)if(this.marked[i][j]===PRIME){this.marked[i][j]=0}}}_findFirstUncoveredZero(){for(let i=0;i<this.n;i++)for(let j=0;j<this.n;j++)if(this.C[i][j]===0&&!this.rowCovered[i]&&!this.colCovered[j])return[i,j];return[-1,-1]}_findStarInRow(row){for(let j=0;j<this.n;j++){if(this.marked[row][j]==STAR){return j}}return-1}_findStarInCol(col){for(let i=0;i<this.n;i++){if(this.marked[i][col]==STAR){return i}}return-1}_findPrimeInRow(row){for(let j=0;j<this.n;j++){if(this.marked[row][j]==PRIME){return j}}return-1}_findSmallestUncovered(){let minval=Number.MAX_SAFE_INTEGER;for(let