This lab is designed to give you experience with:
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:
The program should be named grid and it will accept a minimum of two arguments:
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.
The input file will have the format:
N grid linesEach 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
For this problem you should assume that the grid is sparse and that N could be quite large. As a result it may be extremely inefficient to represent the grid as a two-dimensional matrix. For example, if the grid is 10000x10000 and has only a few hundred occupied squares, then you will waste a great deal of storage on a grid. This could happen for example if the grid represents a pixel image of farmland with a few buildings and each building occupies only a few squares.
In this case it is much better to represent each square as a graph vertex and to store the vertices in a list. If you are asked to count the size of a group containing a square, 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. If you further fail to use some type of graph search algorithm to solve the problem, you will lose additional points.
You should submit the following files: