1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
Recursive Blob Counting
Background:
Consider a 1-D array of (N) “grey” and “white” cells. For example for N = 14:
Connected grey cells form a “Blob” which is like an island in a sea of white background. The above array has 4 “Blobs” of sizes 2 , 1 , 3 , 2 cells, respectively.
To produce this information from the array, we may use a recursive function that examines if a cell (k) is a member of a blob and determines how many cells are in that blob:
int BlobCount (B , N , k)
{
if k is outside the array return 0;
else if (B[k] == white) return 0;
else {
B[k] = white;
return ( 1 + BlobCount ( B , N , k-1 ) + BlobCount ( B , N , k+1 )) ;
};
}
Notice that in this 1-D case, a cell at (k) has two neighbours: one on its left at (k-1) and one on its right at (k+1). Also, notice that a detected grey cell at location (k) is made white so that it would not be counted twice. Then we count it by adding “1” to the sizes of blobs containing its left neighbour at (k-1) and right neighbour at (k+1).
To use the above function, we first copy the original array A to the work array B (so that when we whiten grey cells in B, the array A remains unchanged), and then we examine each element to see if it belongs to a blob.
The Assignment:
In this programming project, you will modify the above algorithm to process a 2-D grid of (N x N) cells representing sea and islands. Sea is represented by cells containing “white” while islands are represented by filled “black” cells. Generate a random array representing this grid (e.g. use N = 20, and approximately 75% sea and 25% land). For example, the given figure shows a grid of size 10 x 10. This grid has 5 islands of sizes 6 , 4 , 8 , 5 , 3
Given a grid of cells, each of which may belong to the sea or to an island, the filled cells that are connected form a Blob (an island). Write a program that will display the grid and find how many islands are in the grid and the area (number of cells) in each.
Design & Implementation Guides
• Global Constants:
The length of the grid side (N)
The characters representing sea and island (e.g. blank and char(219) respectively)
• Global Arrays:
The Grid of size N x N
The copy of the grid of size N x N
The blob counters: a 1-D array of size N2:D
• The needed functions:
void getGrid( ); DONE
A function to generate a random Grid of island cells (25%) and sea cells (75%)
Use srand ((unsigned) time(NULL)); to seed the random number generator.
void displayGrid( ); DONE
A function to display the original grid on the screen.
int BlobCount (N , r , c);
r-1,c-1 r-1,c r-1,c+1
r,c-1 r,c r,c+1
r+1,c-1 r+1,c r+1,c+1
This is a recursive function to receive N, a row (r) and column (c) and work on the cells in the copy grid. If the cell (r,c) is outside the copy grid or if it is “sea”, we return zero, otherwise we set the cell to be “sea” and count it together with the blobs containing its 8 neighbours. Finally, the function returns the number of cells in the blob in which cell (r,c) exists.
Notice that in this 2-D case, a cell at (r,c) has 8 neighbours as shown. This means that the function will be called recursively 8 times.
int Islands( );
This function will do the following:
Copy the original grid to the copy grid
Zero the blob counters array
Initialize the number of islands to zero
For every cell in the copy grid, find the blob count using the previous function. If that count is not zero, save it in the blob counters array and increment the number of islands. Finally, return the number of islands in the grid.
int main( )
The main function will get the original grid, display it and then find the number of islands in the grid and the number of cells in each.
|