Problem

An n × n square consists of black and white cells arranged in a certain way. The problem i...

An n × n square consists of black and white cells arranged in a certain way. The problem is to determine the number of white areas and the number of white cells in each area. For example, a regular 8 × 8 chessboard has 32 one-cell white areas; the square in Figure 1a consists of 10 areas, 2 of them of 10 cells, and 8 of 2 cells; the square in Figure 1b has 5 white areas of 1, 3, 21, 10, and 2 cells.

Write a program that, for a given n × n square, outputs the number of white areas and their sizes. Use an (n + 2) × (n + 2) array with properly marked cells. Two additional rows and columns constitute a frame of black cells surrounding the entered square to simplify your implementation. For instance, the square in Figure 1b is stored as the square in Figure 1c.

Traverse the square row by row and, for the first unvisited cell encountered, invoke a function that processes one area. The secret is in using four recursive calls in this function for each unvisited white cell and marking it with a special symbol as visited (counted).

Figure 1 (a–b) Two n × n squares of black and white cells and (c) an (n + 2) 3 (n + 2) array implementing square (b).

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 5
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT