[Help Me] South African Programming Olympiad 2k+7

I'm really struggling to solve problem #5 from the South African Programming Olympiad 2007. please help by sharing an approach, algorithm or program.

Q5. Rooms

Proposed & Prepared by Marco Gallotta


Fred the manic store-keeper can't keep up with the growing size of his store. He wants to know how many rooms he has and the size of the smallest and largest ones. However, his store is too large for him to work out on his own, so he has asked for your help.


Fred has given you the plans of his store. In the plans, a wall is represented by a '1' and a floor tile by a '0'. Your task is to write a program to group neighbouring floor tiles into rooms. A tile can be grouped together with all tiles one space directly to its left, right, top and bottom. Note that this does not include diagonals. A room is defined as a group of floor tiles that cannot be grouped together with any further floor tiles.

Given the plans your task is to work out:

  1. The total number of rooms
  2. The size of the smallest room
  3. The size of the largest room.


1 <= width, height <= 20.

Sample run

Enter width: 3
Enter height: 2
Enter row: 001
Enter row: 010

Number of rooms: 2
Smallest room: 1
Largest room: 3

Note by Mark Mottian
3 years ago

No vote yet
1 vote

  Easy Math Editor

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold

- bulleted
- list

  • bulleted
  • list

1. numbered
2. list

  1. numbered
  2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1

paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
    # 4 spaces, and now they show
    # up as a code block.

    print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in \( ... \) or \[ ... \] to ensure proper formatting.
2 \times 3 \( 2 \times 3 \)
2^{34} \( 2^{34} \)
a_{i-1} \( a_{i-1} \)
\frac{2}{3} \( \frac{2}{3} \)
\sqrt{2} \( \sqrt{2} \)
\sum_{i=1}^3 \( \sum_{i=1}^3 \)
\sin \theta \( \sin \theta \)
\boxed{123} \( \boxed{123} \)


Sort by:

Top Newest

Perhaps you could reduce this to a graph theory problem. Make an adjacency matrix where each zero in the original data corresponds to a row and column, with 1's in entries corresponding to adjacent zeroes (you could do this in one pass through the given data). Perform matrix operations to order the adjacency matrix into connected components. Each component is one room, and then you just have to pass through the components to find the smallest and largest.

Maggie Miller - 3 years ago

Log in to reply

I am not sure I understand.

Also, the adjacency matrix takes up a lot of space.

Agnishom Chattopadhyay Staff - 2 years, 9 months ago

Log in to reply

  1. Keep a matrix to keep track of which cells have been visited.
  2. While there are still cells unvisited:
    1. Pick the first unvisited cell which is a floor tile
    2. Use Depth First Search to visit all the reachable floors. Remember not to cross walls and keep counting th tiles.

Agnishom Chattopadhyay Staff - 2 years, 9 months ago

Log in to reply

Another example to ensure that you fully understand the problem:

Suppose the plan looks like this:


Number of rooms: 3

Smallest room: 2

Largest room:4

Mark Mottian - 3 years ago

Log in to reply


Problem Loading...

Note Loading...

Set Loading...