0

I have a 2d array created of size n that holds 1s and 0s representing closed and open spaces respectively.

Now I need to test the 2d array to see if it percolates and I'm not sure how to go about this.

I have the following code for creating the array and randomly assigning each spot to a 1 or 0.

int** grid = new int*[boardSize];
for (int i = 0; i < boardSize; ++i) {
    grid[i] = new int[boardSize];
}

for (int i = 0; i < boardSize; i++) {

    for (int j = 0; j < boardSize; j++) {
        if (i == 0) {
            grid[i][j] = 1;
        }
        else if (i == boardSize - 1) {
            grid[i][j] = 1;
        }
        else if (j == 0) {
            grid[i][j] = 1;
        }
        else if (j == boardSize - 1) {
            grid[i][j] = 1;
        }
        else
            grid[i][j] = random(delta);
    }
}

grid[0][enter] = 0;
grid[boardSize - 1][exit] = 0;

This will create an array with closed borders (1s) and put 2 random entry/exit points (0s) on the top and bottom. Only part I'm missing is to test for percolation.

Any help is appreciated, thanks!

Szabolcs Páll
  • 1,402
  • 6
  • 25
  • 31
Probably
  • 426
  • 5
  • 19
  • Have you tried anything? It looks like you already know how to loop through every element of a two dimensional array, so you just need to that but instead of assigning values, compare each element's value of your original grid to the same location of the other grid (probably you should skip the borders and only compare the inner parts). – notmyfriend Nov 17 '15 at 08:09
  • Although it does now occur to me I don't really know what you mean by "test whether it percolates" - so maybe if you can define that more clearly, it'll be clearer to everyone (including you) how to actually implement such a test. – notmyfriend Nov 17 '15 at 08:10
  • As far as I understand, we could interpret the 2d array as a maze with passable terrain (0), walls (1) and a defined entry and exit point (0 in the outer border). The question would be, if there is any way (sequence of adjoining passable terrain pieces) that leads from entry to exit. Any complexity constraint? – grek40 Mar 11 '16 at 14:35

0 Answers0