Lesson 19Lab exercise: eraseobject

 Background:

  An image can be represented as a grid of black and white cells.  Two cells in an image are part of the same object if each is black and there is a sequence of either horizontal or vertical moves from one black cell to another adjacent black cell.  For example, the diagram below represents an image that contains two objects, one of them consisting of a single cell.

                       

 

Many drawing or graphic programs include erasing features.  With such an eraser tool, an object can be completely erased by double-clicking on that object with a mouse.  This problem involves erasing an object stored as a collection of black squares in a matrix. 

Assignment:

1.   The file, ‘Digital.txt’, is the name of a text file with the first line containing the number of pairs to follow.  Line two will contain a pair of integers, separated by a blank space.  This row and column coordinate will specify the location of a Black cell in the starting grid.  Each line thereafter will contain a pair of integers until the end of the file.  The row and column values will range from 1..20.

2.   Write a program which accomplishes the following:

a.  Loads the text file digital.txt as the starting 20 x 20 grid of black and white squares.

b.  Asks the user for the starting coordinate of an attempted erasure.  If this coordinate is part of an object, the program should erase the entire object (change black to white).

c.  Prints out the grid afterward.

3.   Store the information about the image using an enumeration.  The suggested definition is

      enum  cellType  {black, white};

Instructions:

1.   The left hand image is the result of loading the data from digital.txt.  After loading the data file, print out the image.

2.   Erase any one of the objects, print out the remaining two objects.

3.   Erase any one of the remaining two objects, print out the single remaining object.

Image before an erasures:               Image after first erasure:                  Image after second erasure:

   12345678901234567890

 

1  --------------------

2  --------------------

3  -@@@@@@@@@@@@@------

4  -------------@------

5  -------------@------

6  -------------@------

7  -------------@------

8  -------------@------

9  -------------@------

0  ----@@@@@-----------

1  ----@@@@@-----------

2  ----@@@@@-----------

3  ----@@--@-----------

4  ----@--@@-----------

5  ----@@@@@-----------

6  --------------------

7  -------------@@@@---

8  -------------@------

9  ----------@@@@------

0  ----------@---------

 

   12345678901234567890

 

1  --------------------

2  --------------------

3  -@@@@@@@@@@@@@------

4  -------------@------

5  -------------@------

6  -------------@------

7  -------------@------

8  -------------@------

9  -------------@------

0  --------------------

1  --------------------

2  --------------------

3  --------------------

4  --------------------

5  --------------------

6  --------------------

7  -------------@@@@---

8  -------------@------

9  ----------@@@@------

0  ----------@---------

    12345678901234567890

 

  1 --------------------

  2 --------------------

  3 --------------------

  4 --------------------

  5 --------------------

  6 --------------------

  7 --------------------

  8 --------------------

  9 --------------------

 10 --------------------

 11 --------------------

 12 --------------------

 13 --------------------

 14 --------------------

 15 --------------------

 16 --------------------

 17 -------------@@@@---

 18 -------------@------

 19 ----------@@@@------

 20 ----------@---------