在C++中查找二维数组中的最大面积

8

我需要编写一个递归函数,在C ++中查找只包含1或0的2D数组中数字“1”的最大区域。

例子:

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。

我一直在考虑使用类似于泛洪填充算法的方法来实现,但是一直想不出来。


3
泛洪填充应该可以解决问题。如果你在某个地方卡住了,你应该发布你的尝试并描述你的问题。 - Drew Dormann
也许对于每个等于1的元素,检查它的北、南、东、西四个方向,然后再递归地进行相同的操作。同时,将已经递归过的数组索引添加到忽略列表中。有很多种泛洪填充算法,了解哪种算法最好会很有趣。 - james82345
一个相关的问题是https://dev59.com/J3E95IYBdhLWcg3wDpmg - Mihai8
4个回答

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");
}

希望这对您有所帮助 :)

2
我考虑使用类似泛洪填充算法的方式来实现这个功能。
我认为这是一个很好的方法。对于任何一个数字为1的位置应用泛洪填充算法,计数并将它们替换为0。
重复此过程,直到网格完全由0组成。
以下内容将无特定顺序地打印出连接组件的大小:
#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);
}

1
我不确定你想如何处理这个问题,但如果你要用0替换1,就必须小心。否则,你可能会将一个连通的表面切成两半,并且无法再识别它们最初是属于一起的。 - Misch

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;
        }

顺便提一下:OP正在寻求递归解决方案,因此洪水填充和回溯应该可以一起使用。 - taocp

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));
    }
}

谢谢提示,我忘记加入擦除语句了。 - Jo So

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接