1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
// i: current row in the map
// j: current column in the map
// m: map of land and sea, expressed as ints
// N: dimension of the map
void markAllNeighboringLand(int i, int j, int** m, int N)
{
//first check that one row up is in bounds
//then check that the square that's one row up is LAND
if(i-1 >= 0 && m[i-1][j] == 0)
{
//if so...
m[i-1][j] = 2; //mark it as visited
markAllNeighboringLand(i-1, j, m, N); //go do the same check on all its neighbors
}
//same, but for one row down
if(i+1 < N && m[i+1][j] == 0)
{
m[i+1][j] = 2;
markAllNeighboringLand(i+1, j, m, N);
}
//same, but for one column left
if(j-1 >= 0 && m[i][j-1] == 0)
{
m[i][j-1] = 2;
markAllNeighboringLand(i, j-1, m, N);
}
//same, but for one column right
if(j+1 < N && m[i][j+1] == 0)
{
m[i][j+1] = 2;
markAllNeighboringLand(i, j+1, m, N);
}
}
|