Tino APCS

Lab 24.1 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). 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.

Here is a video explaining the flood fill strategy used in erase object.

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 1 to 20.

  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). If the starting coordinate is out of bounds, you program should do nothing (and not crash). Also, if the starting coordinate is on the edge of the board, your program should not crash out of bounds when calling recursion.

    3. Print out the grid afterwards.

    4. Repeat steps (b) and (c) until the user says they do not want to erase again.

    5. Submit your code below

You must Sign In to submit to this assignment

Last modified: April 24, 2024

Back to The Solution To The Maze Problem

Dark Mode

Outline