Question: Write a recursive method that determines the number of organisms in an image (2d array), and label each organism. Description: Given a 2d array of
Write a recursive method that determines the number of organisms in an image (2d array), and label each organism.
Description: Given a 2d array of characters, where an asterisk (*) marks a non-empty cell. An organism is defined as all cells connected directly to other cells in the up/down/left/right (not diagonal) directions.
The method signature above is not a recursive method. Instead, it iterates through the image and as soon as it encounters a new organism, it calls a recursive method (that you define) that labels the newly found organism.
Look at the provided driver code to see how the organism is stored and compare that with the output of the labeled organisms below. In the original image, the organisms are identified by asterisks (*), but the resulting image has each organism labeled with a different character in the alphabet.
Code in python
Provided:
{'*','*',' ',' ',' ',' ',' ',' ','*',' '}, {' ','*',' ',' ',' ',' ',' ',' ','*',' '}, {' ',' ',' ',' ',' ',' ','*','*',' ',' '}, {' ','*',' ',' ','*','*','*',' ',' ',' '}, {' ','*','*',' ','*',' ','*',' ','*',' '}, {' ','*','*',' ','*','*','*','*','*','*'}, {' ',' ',' ',' ',' ',' ',' ',' ','*',' '}, {' ',' ',' ',' ',' ',' ',' ',' ','*',' '}, {' ',' ',' ','*','*','*',' ',' ','*',' '}, {' ',' ',' ',' ',' ','*',' ',' ','*',' '} Expected output:
aa e a e ee b eee bb e e e bb eeeeee e e ddd e d e
My output:
aa b a b cc d ccc dd c c c dd cccccc c c eee c e c
The way I see the program need to count backwards from the bottom most right to the top most left and start with the latest alphabet. However, my code runs from the top most left corner to the bottom most right. Is there any code that can do that way?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
