- 1

Please i need in this algorithm question I came across

Centilytics servers were modeled as an M X N Mesh and needed to be updated, all updated state = 1 and non updated state = 0. Now only the updated server will be able to update adjacent servers and no server must be updated twice. You are to write the number of days that it will take to update all the servers of a given N X M Eg. Day One [1 0 0] [0 0 0] [0 0 1] After day one the state would become [1 1 0] [1 0 1] [0 1 1]

20th Jan 2022, 11:10 AM
Ajisegiri Sunday
Ajisegiri Sunday - avatar
2 odpowiedzi
0
Something like ....foreach(int i: i<N:i++){foreach(int j: i<M:j++) { if (mesh_k(i,j)==1) mesh_k+1(i,j+1)=1}
23rd Jan 2022, 4:14 PM
Cristian Baeza Jimenez
Cristian Baeza Jimenez - avatar
0
package com.owoez.vgsa.assignmenttwo; import java.util.Arrays; public class NumberOfDays { static void printArray(int[][] grid){ int M = grid.length; int N = grid[0].length; for(int i = 0; i < M; i++){ for(int j = 0; j < N; j++){ System.out.print(grid[i][j] + " "); } System.out.println(); } } static int[][] copyArray(int[][] copyFrom, int[][] copyTo){ for (int i = 0; i < copyFrom.length; i++) { copyTo[i] = Arrays.copyOf(copyFrom[i], copyFrom[i].length); } return copyTo; } static int calcNumOfDays(int[][] grid){ if(grid.length <= 0){ return 0; } int numberOfDays = 0; int M = grid.length; int N = grid[0].length; int[][] updateState = new int[M][N]; updateState = copyArray(grid, updateState); boolean traverse = true; while(traverse){ boolean updated = false; for(int i = 0; i < M; i++){ for(int j = 0; j < N; j++){ if(grid[i][j] == 1){ //check left if(j - 1 >= 0){ if(grid[i][j-1] == 0){ updateState[i][j-1] = 1; updated = true; } } //check right if(j + 1 < N){ if(grid[i][j+1] == 0){ updateState[i][j+1] = 1; updated = true; } } //check top if(i - 1 >= 0){ if(grid[i-1][j] == 0){ updateState[i-1][j] = 1; updated = true; } } //check bottom if(i + 1 < M){ if(grid[i+1][j] == 0){ updateState[i+1][j] = 1; updated = true; } } } } } if(updated == true){ numberOfDays += 1; grid = copyArray(updateState, grid); }else{ traverse = false; } } return numberOfDays; } public static void main(String[] args) { int[][] graphTest = {{0,0,0},{0,1,0},{0,0,0},{0,0,1}}; int[][] graphTest2 = {{0,0,0},{0,1,0},{0,0,0},{0,0,0}}; int[][] graphTest3 = {{1,0,0},{0,0,0},{0,0,0},{0,0,0}}; int[][] graphTest4 = {{1,1,1},{1,1,1},{1,1,1},{0,0,0}}; System.out.println(calcNumOfDays(graphTest)); System.out.println(calcNumOfDays(graphTest2)); System.out.
6th Mar 2022, 1:52 PM
Tobiloba Owolabi