Skip to main content
ICT
Lesson AB24 - Recursive Array Programming
 
Main   Previous
 

LAB ASSIGNMENT AB24.1 page 7 of 7

EraseObject

Background:

An image can be represented as a grid of black and white cells. An object in the image is defined as a group of connected black cells that touch on one side (Please note: cells are not connected if they only touch at a corner). They must touch along their sides. The diagram below represents an image that contains two objects, therefore, and one of them consists of a single cell.

A way to test whether a group of cells is an object (within an image) is to ask:

  • Can you move from any black cell in the group to any other black cell in the group by moving one square at a time, keeping on black cells the whole way?
  • Each move must be up, down, left, or right; diagonal moves are not allowed.
  • If you can, then the group of black cells is an object (not to be confused with a Java object!).

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 image.

Assignment:

  1. The first entry in the provided text file (digital.txt) is the number of pairs that follow (i.e., 55). Each subsequent line contains a pair of integers, separated by a blank space. Each pair is a row and column coordinate that specifies the location of a black cell in the starting grid. The row and column values range from 0 to 19.

  2. Write a program that accomplishes the following:

    1. Load the text file digital.txt representing the 20 x 20 grid of black and white squares.

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

    3. Print out the grid afterwards.

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. (NOTE: Row and column values in pictures should be from 0 to 19)

 

Main   Previous
Contact
 © ICT 2006, All Rights Reserved.