Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(){
- const SEED = BigInt('0xDEADBEEF');
- const ROWS = 100;
- const COLS = 100;
- const COLORS_NUM = 3;
- let LCGRand = function() {
- this.next = SEED;
- this.A = BigInt('6364136223846793005');
- this.C = BigInt('1442695040888963407');
- this.M = BigInt('0xFFFFFFFFFFFFFFFF');
- this.SHIFT = BigInt('32');
- return function() {
- this.next = (((this.next * this.A) & this.M) + this.C) & this.M;
- return this.next >> this.SHIFT;
- };
- }();
- function matrixAt(matrix, i, j) {
- return matrix[i * COLS + j];
- }
- function generateMatrix() {
- let matrix = [];
- for (let i = 0; i < ROWS * COLS; i++) {
- matrix.push({
- color: Number(LCGRand()) % COLORS_NUM,
- visited: false
- });
- }
- return matrix;
- }
- function isValidCoords(i, j) {
- return (i >= 0) && (i < ROWS) && (j >= 0) && (j < COLS);
- }
- function visitNode(matrix, i, j) {
- const MOVES = [
- [ -1, 0 ],
- [ +1, 0 ],
- [ 0, -1 ],
- [ 0, +1 ],
- ];
- let vec = [[i, j]];
- let blockSize = 0;
- let currentNode = matrixAt(matrix, i, j);
- const targetColor = currentNode.color;
- currentNode.visited = true;
- while (vec.length > 0) {
- [i, j] = vec.pop();
- blockSize++;
- for (let move of MOVES) {
- let movedI = i + move[0], movedJ = j + move[1];
- if (isValidCoords(movedI, movedJ)) {
- let node = matrixAt(matrix, movedI, movedJ);
- if (node.color === targetColor && !node.visited) {
- node.visited = true;
- vec.push([movedI, movedJ]);
- }
- }
- }
- }
- return blockSize;
- }
- function main() {
- let matrix = generateMatrix();
- let maxBlockSize = 0;
- let startTime = performance.now();
- for (let i = 0; i < ROWS; i++) {
- for (let j = 0; j < COLS; j++) {
- if (!matrixAt(matrix, i, j).visited) {
- const blockSize = visitNode(matrix, i, j);
- if (maxBlockSize < blockSize) {
- maxBlockSize = blockSize;
- }
- }
- }
- }
- console.log('Max block size: ' + maxBlockSize);
- console.log('Time: ' + (performance.now() - startTime));
- }
- main();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement