Question

I'm am trying to create a recursive subprogram using a personalized header file to recursively fill in an array. The array is initially filled with 0's, and by the end, is filled with all ones, at least when appropriate. I set it up so that each time the subprogram is called, it checks all points, around it, including diagonally. However, it doesn't fill all the way. I'm assuming it might be the order in which I've put my if statements in, but I've yet to figure out how to get it to continue recursively filling.

I know that's its stopping because at some point, it detects no further points around it that are not filled, which is not true. I am asking what logical flaw I have created, because so far I haven't found it. Thank you.

Notes about the predefined methods: setCoordinates() sets the space specified to the value selected. getCoordinates retrieves the coordinates from the Grid class in vector< vector > form.

Edit: I deleted the else part of the else if statements, and everything gets flood filled EXCEPT when the leading Coordinate is 0. I'm still looking into this.

Edit2: Apparently by adding the if statement:

if(i == 0)
{
    i1 = i + 1;
    vec.setCoordinates(i1, j, 1);
    Fill_image(vec, i1, j);
}

After all of the other if statements makes it work if 0 is the leading coefficient. I'll check to see what ramifications this will have in the morning when trying to fill a specific image, but all I wanted to get working now was a simple 0 matrix fill.

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include "Grid.h"

using namespace std;

void Fill_image(Grid & vec, int i, int j);

int main()
{
    int size_Column, size_Row, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 =     
    0, x, y;

  cout << "Enter the size you want the rows and columns to be: ";
  cin >> size_Row >> size_Column;

  vector< vector<int> > Image(size_Row);
  vector<int> Rows(size_Column);

  while(count3 < Image.size())
  {
     Image[count3] = Rows;
     count3 += 1;
  }

  while(count1 < Image.size())
  {
     while(count2 < Rows.size())
     {
       Image[count1][count2] = 0;
       cout << setw(3) << Image[count1][count2];
       count2 += 1;
     }
     cout << endl;
     count1 += 1;
     count2 = 0;
  }

   cout << "Enter the coordinates you want to start out with: ";
   cin >> x >> y;

   Grid Board(Image);
   Fill_image(Board, x, y);

   while(count4 < Image.size())
   {
     while(count5 < Rows.size())
     {
       cout << setw(3) << Board.getCoordinates()[count4][count5];
       count5 += 1;
     }
     cout << endl;
     count4 += 1;
     count5 = 0;
   }

   return 0;
}


void Fill_image(Grid & vec, int i, int j)
{
     vector< vector<int> > board = vec.getCoordinates();
     vector<int> Placeholder = board[0];

     int j1 = j + 1;
     int j2 = j - 1;
     int i1 = i + 1;
     int i2 = i - 1;

     if(j1 > Placeholder.size() - 1)
     {
       j1 = j;
     }
     if(j2 < 0)
     {
       j2 = j;
     }
     if(i1 > board.size() - 1)
     {
       i1 = i;
     }
     if(i2 < 0)
     {
       i1 = i;
     }
     if(j < Placeholder.size() && j >= 0 && i < board.size() && i >= 0 && board[i][j] == 
0)
     {
       vec.setCoordinates(i, j, 1);
       Fill_image(vec, i, j);
     }
     else if(j1 < Placeholder.size() && board[i][j1] == 0)
     {
       vec.setCoordinates(i, j1, 1);
       Fill_image(vec, i, j1);
     }
     else if(j2 >= 0 && board[i][j2] == 0)
     {
       vec.setCoordinates(i, j2, 1);
       Fill_image(vec, i, j2);
     }
     else if(i1 < board.size() && board[i1][j] == 0)
     {
       vec.setCoordinates(i1, j, 1);
       Fill_image(vec, i1, j);
     }

     else if(i2 >= 0 && board[i2][j] == 0)
     {
       vec.setCoordinates(i2, j, 1);
       Fill_image(vec, i2, j);
     }
     else if(i2 >= 0 && j2 >= 0 && board[i2][j2] == 0)
     {
       vec.setCoordinates(i2, j2, 1);
       Fill_image(vec, i2, j2);
     }
     else if(i2 >= 0 && j1 < Placeholder.size() && board[i2][j1] == 0)
     {
       vec.setCoordinates(i2, j1, 1);
       Fill_image(vec, i2, j1);
     }
     else if(j2 >= 0 && i1 < board.size() && board[i1][j2] == 0)
     {
       vec.setCoordinates(i1, j1, 1);
       Fill_image(vec, i1, j2);
     }
     else if(i1 < board.size() && j1 < Placeholder.size() && board[i1][j1] == 0)
     {
       vec.setCoordinates(i1, j1, 1);
       Fill_image(vec, i1, j1);
     }

}
Was it helpful?

Solution

You really need to run this through the debugger.

Your recursion will only follow one path each time, as you are using else if statements. If it finds one path that is free it will follow it and only that one. This works OK in your current case (with the fix below), as it can just go across the board and down, but it will fail with a more complicated board (e.g. one with walls).

Your actual problem is a simple typo, which you would be able to find with a debugger by looking at the values of i, i1 and i2 when the program is running.

This piece of code:

 if(i2 < 0)
 {
   i1 = i;
 }

checks against i2 but changes i1. This means when you have i as 0, i1 is set to 1, i2 is set to -1 and then i1 is set back to 0 again. So your program can never move down the board.

It should be

 if(i2 < 0)
 {
   i2 = i;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top