在c ++中查找2d数组中的最大区域

时间:2013-03-29 14:40:19

标签: c++ arrays 2d area

我需要在c ++中编写递归函数,在2d数组中找到最大区域的数字'1',只包含1或0。

示例:

int Arr[5][8] =
{
{ 0, 0, 0, 0, 1, 1, 0, 0, },
{ 1, 0, 0, 1, 1, 1, 0, 0, },
{ 1, 1, 0, 1, 0, 1, 1, 0, },
{ 0, 0, 0, 1, 1, 1, 1, 0, },
{ 0, 1, 1, 0, 0, 0, 0, 0, },
};

视觉示例:http://s23.postimg.org/yabwp6h23/find_largest.png

此阵列的最大区域为12,第二大区域为3,第三大区域为2。

我正在考虑使用类似于泛洪填充算法的方法,但是无法弄清楚如何。

4 个答案:

答案 0 :(得分:3)

bool visited[5][8];
int i,j;
// variables for the area:
int current_area = 0, max_area = 0;
int Arr[5][8]={ // type your map of values here
}

// functions

void prepare_visited_map() {
    for(i=0;i<5;i++) {
        for(j=0;j<8;j++) visited[i][j] = false;
    }
}

// recursive function to calculate the area around (x,y)
void calculate_largest_area(int x, int y) {
    if(visited[x][y]) return;
    // check if out of boundaries
    if(x<0 || y<0 || x>=5 || y>=8) return;
    // check if the cell is 0
    if(!Arr[x][y]) {
        visited[x][y] = true;
        return;
    }

    // found a propper cell, proceed
    current_area++;
    visited[x][y] = true;
    // call recursive function for the adjacent cells (north, east, south, west)
    calculate_largest_area(x,y-1);
    calculate_largest_area(x+1,y);
    calculate_largest_area(x,y+1);
    calculate_largest_area(x-1,y);
    // by the end of the recursion current_area will hold the area around the initial    cell
}

// main procedure where the above functions are used
int mian() {
    // calculate the sorrounding area of each cell, and pick up the largest of all results
    for(i=0;i<5;i++) {
        for(j=0;j<8;j++) {
            prepare_visited_map();
            calculate_largest_area(i,j);
            if(current_area > max_area)   max_area = current_area;
        }
    }
    printf("Max area is %d",max_area");
}

希望这有用:)

答案 1 :(得分:2)

  

我正在考虑用类似洪水填充算法的方法来做这件事

我认为这是一个非常好的方法。将洪水填充应用于任何1,对其进行计数并将其替换为零。

重复直到网格完全由零组成。

以下内容将按照特定顺序打印出连接组件的大小:

#include <iostream>

constexpr int N = 5;
constexpr int M = 8;

int arr[N][M] =
{
{ 0, 0, 0, 0, 1, 1, 0, 0, },
{ 1, 0, 0, 1, 1, 1, 0, 0, },
{ 1, 1, 0, 1, 0, 1, 1, 0, },
{ 0, 0, 0, 1, 1, 1, 1, 0, },
{ 0, 1, 1, 0, 0, 0, 0, 0, },
};

int fill(int arr[N][M], int r, int c) {
  int count = 0;
  if (r < N && arr[r][c]) {
    for (int i = c; i >= 0 && arr[r][i]; --i) {
      arr[r][i] = 0;
      count += fill(arr, r + 1, i) + 1;
    }
    for (int i = c + 1; i < M && arr[r][i]; ++i) {
      arr[r][i] = 0;
      count += fill(arr, r + 1, i) + 1;
    }
  }
  return count;
}

int print_components(int arr[N][M]) {
  for (int r = 0; r < N; ++r) {
    for (int c = 0; c < M; ++c) {
      if (arr[r][c]) {
        std::cout << fill(arr, r, c) << std::endl;
      }
    }
  }
}

int main() {
  print_components(arr);
}

答案 2 :(得分:1)

之类的,

int max_area = 0;

foreach y
    foreach x
        if (pos[y][x] == 1  &&  !visited[y][x])
        {
            int area = 0;
            Queue queue = new Queue();
            queue.push(new Point(x, y));
            visited[y][x] = true;

            while (!queue.empty())
            {
                Point pt = queue.pop();
                area++;

                foreach neightboor of pt (pt.x±1, pt.y±1)
                    if (pos[neightboor.y][neightboor.x] == 1  &&  !visited[neightboor.y][neightboor.x])
                    {
                        visited[neightboor.y][neightboor.x] = true;
                        queue.push(new Point(neightboor.x, neightboor.y));
                    }
            }

            if (area > max_area)
                max_area = area;
        }

答案 3 :(得分:1)

快速接近,但我不知道是否有办法以理智的方式做到这一点(递归 对于C ++,调用每个元素都不会缩放,因为调用堆栈是有限的)

int maxy = 5
int maxx = 8

int areasize(int x, int y) {
    if (x < 0 || y < 0 || x > maxx || y > maxy || !Arr[y][x])
        return 0;

    Arr[y][x] = 0;

    return 1
           + areasize(x + 1, y)
           + areasize(x - 1, y)
           + areasize(x, y + 1)
           + areasize(x, y - 1);
}

maxarea = 0;

for (int y = 0; y < maxy; y++) {
    for (int x = 0; x < maxx; x++) {
        maxarea = std::max(maxarea, areasize(x, y));
    }
}