Advertisement
Guest User

Untitled

a guest
Feb 8th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(){
  2.     const SEED = BigInt('0xDEADBEEF');
  3.     const ROWS = 100;
  4.     const COLS = 100;
  5.     const COLORS_NUM = 3;
  6.  
  7.     let LCGRand = function() {
  8.         this.next = SEED;
  9.         this.A = BigInt('6364136223846793005');
  10.         this.C = BigInt('1442695040888963407');
  11.         this.M = BigInt('0xFFFFFFFFFFFFFFFF');
  12.         this.SHIFT = BigInt('32');
  13.         return function() {
  14.             this.next = (((this.next * this.A) & this.M) + this.C) & this.M;
  15.             return this.next >> this.SHIFT;
  16.         };
  17.     }();
  18.  
  19.     function matrixAt(matrix, i, j) {
  20.         return matrix[i * COLS + j];
  21.     }
  22.        
  23.     function generateMatrix() {
  24.         let matrix = [];
  25.         for (let i = 0; i < ROWS * COLS; i++) {
  26.             matrix.push({
  27.                 color: Number(LCGRand()) % COLORS_NUM,
  28.                 visited: false
  29.             });
  30.         }
  31.         return matrix;
  32.     }
  33.  
  34.     function isValidCoords(i, j) {
  35.         return (i >= 0) && (i < ROWS) && (j >= 0) && (j < COLS);
  36.     }
  37.  
  38.     function visitNode(matrix, i, j) {
  39.         const MOVES = [
  40.             [ -1, 0 ],
  41.             [ +1, 0 ],
  42.             [ 0, -1 ],
  43.             [ 0, +1 ],
  44.         ];
  45.        
  46.         let vec = [[i, j]];
  47.         let blockSize = 0;
  48.         let currentNode = matrixAt(matrix, i, j);
  49.         const targetColor = currentNode.color;
  50.         currentNode.visited = true;
  51.         while (vec.length > 0) {
  52.             [i, j] = vec.pop();
  53.             blockSize++;
  54.            
  55.             for (let move of MOVES) {
  56.                 let movedI = i + move[0], movedJ = j + move[1];
  57.                 if (isValidCoords(movedI, movedJ)) {
  58.                     let node = matrixAt(matrix, movedI, movedJ);
  59.                     if (node.color === targetColor && !node.visited) {
  60.                         node.visited = true;
  61.                         vec.push([movedI, movedJ]);
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         return blockSize;
  67.     }
  68.  
  69.     function main() {
  70.         let matrix = generateMatrix();
  71.         let maxBlockSize = 0;
  72.        
  73.         let startTime = performance.now();
  74.         for (let i = 0; i < ROWS; i++) {
  75.             for (let j = 0; j < COLS; j++) {
  76.                 if (!matrixAt(matrix, i, j).visited) {
  77.                     const blockSize = visitNode(matrix, i, j);
  78.                     if (maxBlockSize < blockSize) {
  79.                         maxBlockSize = blockSize;
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.        
  85.         console.log('Max block size: ' + maxBlockSize);
  86.         console.log('Time: ' + (performance.now() - startTime));
  87.     }
  88.    
  89.     main();
  90. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement