Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- let solution_1 = function(seedBn, rows, cols, colorsNum) {
- const SEED = BigInt(seedBn);
- const ROWS = rows;
- const COLS = cols;
- const COLORS_NUM = colorsNum;
- 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;
- }
- }
- }
- }
- return maxBlockSize;
- }
- return main();
- };
- let solution_2 = function(seedBn, rows, cols, colorsNum) {
- const SEED = BigInt(seedBn);
- const ROWS = rows;
- const COLS = cols;
- const COLORS_NUM = colorsNum;
- 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 generateMatrix(size) {
- let matrix = [];
- for (let i = 0; i < size; i++) {
- matrix.push({
- color: Number(LCGRand()) % COLORS_NUM
- ,area: {
- pos: i
- }
- });
- }
- return matrix;
- }
- function matrixAt(matrix, i, j) {
- return matrix[i * COLS + j];
- }
- function main()
- {
- var len = ROWS * COLS;
- let matrix = generateMatrix(len);
- let startTime = performance.now();
- for (var i = 0; i < ROWS; i++) {
- var prev=null;
- for (var j = 0; j < COLS; j++)
- {
- var curr = matrixAt(matrix, i, j);
- if (prev && curr.color == prev.color){
- curr.area = prev.area;
- }
- if (i>0){
- var top = matrixAt(matrix, i-1, j);
- if (top.color == curr.color){
- if (top.area.pos!=curr.area.pos)
- merge(top, curr);
- }
- }
- prev=curr;
- }
- }
- var area = new Array(len).fill(0);
- var maxArea = 0;
- for (var i = 0; i < len; i++) {
- var pos=matrix[i].area.pos;
- area[pos]+=1;
- if (area[pos] > maxArea) {
- maxArea = area[pos];
- }
- }
- return maxArea;
- }
- function merge(a,b)
- {
- // a.area.size += b.area.size;
- // b.area.size = a.area.size;
- a.area.pos = Math.min(a.area.pos, b.area.pos)|0 ;
- b.area.pos = a.area.pos;
- }
- return main();
- };
- const BASE_SEED = BigInt('0xDEADBEEF');
- for (let rows = 1; rows < 50; rows++) {
- for (let cols = 1; cols < 100; cols++) {
- for (let seedOffset = BigInt(-5); seedOffset < BigInt(5); seedOffset++) {
- const seed = BASE_SEED + seedOffset;
- const result_1 = solution_1(seed, rows, cols);
- const result_2 = solution_2(seed, rows, cols);
- if (result_1 != result_2) {
- console.log('ERROR FOR SEED = ' + seed.toString()
- + ', ROWS = ' + rows.toString()
- + ', COLS = ' + cols.toString()
- + ' (' + result_1.toString() + ' != ' + result_2.toString() + ')');
- return;
- }
- }
- }
- }
- console.log('OK');
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement