Area of the room
Your task is to write a program that will find the area of room in a given square maze.

Note. Use recursion for solving this problem.

Input:
First line contains only one number N (3 <= N <= 10).The number that describes the size of the square maze.
On the next N lines maze itself is inputed ('.' - empty cell,'*' - wall).
And the last line contains two numbers - the row and column of the cell in the room those area you need to find.It is guaranteed that given cell is empty and that the maze is surrounded by walls on all sides.

Output:
Output only one number - total amount of empty cells in the selected room.

Samples:

Input

5
*****
**..*
*.*.*
*..**
*****
2 4
Output 3

i have no idea how to write the code, can you help me?!
Last edited on
Just checking - the given row and column for your sample start counting at 1, correct? (Instead of 0, in which case 2 is actually the third row.)

For recursion: you're guaranteed that the given spot is an empty cell, so somehow, check the four "sides" of that cell (above, left, below, right), determining which of those are empty. If a neighbor is empty, call this function on it, to check if it has empty neighbors as well. While in this recursive state, you must keep updating the total size of the room.

You also need to keep track of which cells have already been counted, to avoid counting cells twice. Example - count the initial cell, find an empty neighbor, then count the initial cell again, because it is an empty neighbor of its neighbor. To do this, I'd suggest changing the content of an already-checked cell to a third character (as in, not "*" or ".").
This is a simple flood fill problem:
en.wikipedia.org/wiki/Flood_fill
I understand how to solve, but i have no idea how to write it
Writing it is a matter of understanding the language. What part of writing it is your problem? (i.e. the recursion, the 2D array, etc.)
wikipedia wrote:
Flood-fill (node, target-color, replacement-color):
1. If the color of node is not equal to target-color, return.
2. Set the color of node to replacement-color.
3. Perform Flood-fill (one step to the west of node, target-color, replacement-color).
Perform Flood-fill (one step to the east of node, target-color, replacement-color).
Perform Flood-fill (one step to the north of node, target-color, replacement-color).
Perform Flood-fill (one step to the south of node, target-color, replacement-color).
4. Return.

EDIT: Who reported trojansdestroy's post? Why would someone report that post?
Last edited on
Probably a mis-click. Report != Reply but they're damn close.
closed account (D80DSL3A)
Reporting a post requires more than 1 click. A dialog box appears for giving a reason and there is a cancel button.
My first reported post! I'm growing up so fast!

In all seriousness, I'd like to know what (if anything) was entered into the input field in that box.
Last edited on
They should say: "Reported by [insert name here]"
Topic archived. No new replies allowed.