CS302 -- Lab 9

Grids


Lab Objective

This lab is designed to give you experience with:

  1. graphs, and
  2. algorithm selection


Problem Statement

Consider an N-by-N grid in which some squares are occupied by circles. Two occupied squares belong to the same group if they share a common edge. In the grid shown below, there is one group of four occupied squares, three groups of two occupied squares, and two individual occupied squares. Note that diagonal squares are not considered to share a common edge.

Write a program that does the following:

  1. Computes the size of a group when a square in the group is given.
  2. Computes the number of different groups.
  3. Lists all groups.

The program should be named grid and it will accept a minimum of two arguments:

  1. the name of the input grid file
  2. one of the following three flags:
    1. -c: compute the number of different groups
    2. -l: list all groups
    3. -s row col: compute the size of the group with the indicated square. If the square is unoccupied the program should print a message that indicates that the square is unoccupied and that the group size is therefore 0.
An executable, called grid, can be found in the lab 9 directory. You should use it to see how you should format your output. Note that vertices are represented using the notation gx.y where g stands for grid, x stands for the row, and y stands for the column.

The file catches a number of errors because you may not enter arguments correctly or create a correct grid file. However, because time is short, your program may assume that all arguments are entered correctly and that the grid file is correct.


Format of the Input File

The input file will have the format:

N
grid lines
Each grid line represents one line in the grid. A blank will denote an empty square and an X will denote a square with a circle. The above grid would be represented by the following file, named grid1:
10
         X
   XX    X
          
    X  X  
   X   X  
       XX 
    XX    
          
          
          


Storage Compactness

Because you are only required to handle one option in any execution of the program it is extremely inefficient to represent the grid as a two-dimensional matrix. It is much better to keep the vertices in a list and, if you are asked to count the size of a group containing a square, to save a pointer to the vertex representing that square when the vertex is created. You can compute the edges for each vertex by keeping only the current line and the previous line of the grid when you are reading in the grid. Thus you only need a two row matrix. It is permissable to use a full blown two-dimensional matrix but you will lose the number of points shown in the grading guide if you do so.


What to Submit

You should submit the following files:

  1. grid.h
  2. grid.cpp
  3. makefile
  4. grid1