Question
JAVA - If I have a 2D array of a grid of numbers that were randomly populated (1 or 0), what is the iterative algorithm
JAVA - If I have a 2D array of a grid of numbers that were randomly populated (1 or 0), what is the iterative algorithm in order to edit and output the grid with colonies like the second photo? Please read more information below.
char[][] grid = { { '0', '0', '0', '1', '1', '0', '1', '1', '0', '0', '0', '1', '1', '1', '0', '1', '1', '1', '0', '1' }, { '1', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1' }, { '0', '1', '0', '0', '1', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '1'}, {'1', '1', '1', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '1', '1', '0', '1', '1', '0'}, {'0', '1', '1', '1', '0', '1', '1', '1', '0', '1', '0', '0', '1', '0', '1', '0', '1', '1', '0', '1' } };
Colonies are formed by neighboring cells. Two cells are considered neighbors, if their locations are adjacent either horizontally, vertically, or diagonally. Thus, the maximum number of neighbors a cell can have is 3 for a corner cell, 5 for a cell on a single border, and 8 for a cell not on the borders.
Create a method called ExploreAndLabelColony which takes input of grid, the coordinates of a starting location, and a letter, ExploreAndLabelColony will either label that location and all its neighbors or do nothing. Perhaps we can iterate through the alphabet by creating an alphabet char = 'ABC...XYZ' and calling charAt(k), where k is an integer that increases after each colony is found?
Another method called ColonyExplorer will call ExploreAndLabelColony, and ColonyExplorer will be used to populate the entire grid will colonies, whereas ExploreAndLabelColony will do a specific part given coordinates. Thus, we can use ColonyExplorer as an iterative solution to do the entire grid by calling ExploreAndLabelColony.
Can you write JAVA code (iterative, not recursive algorithm) to create the table labeled with A B C D... like the image above? Thank you.
o o o 1 1 0 1 1 o o o 1 1 1 0 1 1 1 o 1 1 o o o o 1 o o o 1 o o o o o 1 1 1 1 1 0 1 0 0 1 0 0 0 1 0 1 0 0 o o o o 1 1 1 1 1 1 0 0 1 1 0 0 0 0 1 0 1 1 0 1 0 1 0 1 oo 1 o 10 1 1 1 1 1 1 1 1 1 1 C D A -- - D A - - C ccc D D D A - - E D D D - F 1 E : 14 : 3 C: 20 D: 8 : 2 F: 1 The information shows that there are six colonies labeled A, B, C, D, E, and F on the grid with size 14, 3, 20, 8, 2, and 1, respectively
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started