/*
 * Solution to Problem 2 (C++)
 * 12th Annual CS Programming Contest
 * WCU Dept of Math and CS
 * Copyright 2001 , All Rights Reserved
 *
 * Problem Statement:
 * Recursive Image Erasing.
 * This program recursively erases a feature from an image. Suppose
 * that an image is represented by a two-dimensional array of
 * values where each value can either be black or white. Contiguous
 * array elements that are black form a feature. Erasing a feature
 * means to turn all the array elements of the feature to the value
 * white. 
 *
 * The solution uses a member function called eraseObject that is
 * called recursively to perform the erasing. The function 
 * eraseObject is passed the array as an argument and row and 
 * column positions as arguments. If the row and column positions
 * specify a position in a feature, that feature is remvoed by
 * recursively calling eraseObject. If the row and column positions
 * specify a position that is not in a feature, the eraseObject
 * function just returns.
 */

#include <iostream.h>
const int NUM_ROWS = 4;

void eraseObject(char image[][NUM_ROWS], int row, int column);

int main()
{
  int row, column;
  bool tokenExists = false;
  char nextChar;
  char image[NUM_ROWS][NUM_ROWS]; 

  cout << "The image is represented by a 4 x 4 array of Bs and Ws" << endl;
  for (row = 0; row < NUM_ROWS; row++) {
    cout << "Enter the characters for row " << row << "." << endl;
    for (column = 0; column < NUM_ROWS; column++)
      cin >> image[row][column];
  } 
  cout << "Enter the row and column of the initial erasure position" << endl;
  cin >> row >> column;
  eraseObject(image, row, column);
  
  cout << "The final image is: " << endl;
  for (row = 0; row < NUM_ROWS; row++) {
    for (column = 0; column < NUM_ROWS; column++)
      cout << image[row][column];
    cout << endl;
  }
}

// recursively erase a feature if the row and column specified are
// part of a feature
void eraseObject(char image[][NUM_ROWS], int row, int column)
{
  if ((row >= 0) && (row < NUM_ROWS) && (column >= 0) && (column < NUM_ROWS))
    if (image[row][column] == 'B') {
      image[row][column] = 'W';
      eraseObject(image, row - 1, column);
      eraseObject(image, row + 1, column);
      eraseObject(image, row, column - 1);
      eraseObject(image, row, column + 1);
    }
}


