📜  C ++程序查找所有填充为0的矩形

📅  最后修改于: 2022-05-13 01:55:40.480000             🧑  作者: Mango

C ++程序查找所有填充为0的矩形

我们有一个二维数组,用零和一填充。我们必须找到所有填充为 0 的矩形的起点和终点。假设矩形是分开的并且不相互接触,但是它们可以接触数组的边界。一个矩形可能只包含一个元素。
例子:

input = [
            [1, 1, 1, 1, 1, 1, 1],
            [1, 1, 1, 1, 1, 1, 1],
            [1, 1, 1, 0, 0, 0, 1],
            [1, 0, 1, 0, 0, 0, 1],
            [1, 0, 1, 1, 1, 1, 1],
            [1, 0, 1, 0, 0, 0, 0],
            [1, 1, 1, 0, 0, 0, 1],
            [1, 1, 1, 1, 1, 1, 1]
        ]


Output:
[
  [2, 3, 3, 5], [3, 1, 5, 1], [5, 3, 6, 5]
]

Explanation:
We have three rectangles here, starting from 
(2, 3), (3, 1), (5, 3)

Input = [
            [1, 0, 1, 1, 1, 1, 1],
            [1, 1, 0, 1, 1, 1, 1],
            [1, 1, 1, 0, 0, 0, 1],
            [1, 0, 1, 0, 0, 0, 1],
            [1, 0, 1, 1, 1, 1, 1],
            [1, 1, 1, 0, 0, 0, 0],
            [1, 1, 1, 1, 1, 1, 1],
            [1, 1, 0, 1, 1, 1, 0]
        ]


Output:
[
  [0, 1, 0, 1], [1, 2, 1, 2], [2, 3, 3, 5], 
  [3, 1, 4, 1], [5, 3, 5, 6], [7, 2, 7, 2], 
  [7, 6, 7, 6]
]

第 1 步:逐行和逐列查找 0
步骤 2:当遇到任何 0 时,将其位置保存在输出数组中,并使用循环将所有与该位置相关的 0 更改为任意公共数字,以便我们下次将其排除在处理之外。
第 3 步:当您在第 2 步中更改所有相关的 0 时,将最后处理的 0 的位置存储在输出数组中的同一索引中。
第 4 步:当你触摸边缘时要特别小心,不要减去 -1,因为循环在确切的位置断裂。
以下是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
void findend(int i,int j, vector> &a, 
             vector> &output,int index)
{
  int x = a.size();
  int y = a[0].size();
  
  // flag to check column edge case,
  // initializing with 0
  int flagc = 0;
  
  // flag to check row edge case,
  // initializing with 0
  int flagr = 0;
  int n, m;
  
  for (m = i; m < x; m++)
  {
  
    // loop breaks where first 1 encounters
    if (a[m][j] == 1)
    {
      flagr = 1; // set the flag
      break;
    }
  
    // pass because already processed
    if (a[m][j] == 5) continue;
  
    for (n = j; n < y; n++)
    {
      // loop breaks where first 1 encounters
      if (a[m][n] == 1)
      {
        flagc = 1; // set the flag
        break;
      }
  
      // fill rectangle elements with any
      // number so that we can exclude
      // next time
      a[m][n] = 5;
    }
  }
  
  if (flagr == 1)
    output[index].push_back(m-1);
  else
    // when end point touch the boundary
    output[index].push_back(m);
  
  if (flagc == 1)
    output[index].push_back(n-1);
  else
    // when end point touch the boundary
    output[index].push_back(n);
}
  
void get_rectangle_coordinates(vector> a)
{
  
  // retrieving the column size of array
  int size_of_array = a.size();
  
  // output array where we are going
  // to store our output
  vector> output;
  
  // It will be used for storing start
  // and end location in the same index
  int index = -1;
  
  for (int i = 0; i < size_of_array; i++)
  {
    for (int j = 0; j < a[0].size(); j++)
    {
      if (a[i][j] == 0)
      {
  
        // storing initial position
        // of rectangle
        output.push_back({i, j});
  
        // will be used for the
        // last position
        index = index + 1;
        findend(i, j, a, output, index);
      }
    }
  }
  
  cout << "[";
  int aa = 2, bb = 0;
  
  for(auto i:output)
  {
    bb = 3;
    cout << "[";
    for(int j:i)
    {
      if(bb)
        cout << j << ", ";
      else
        cout << j;
      bb--;
    }
    cout << "]";
    if(aa) 
      cout << ", ";
    aa--;
  
  }
  cout << "]";
}
  
// Driver code
int main()
{
  vector> tests = {
    {1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 0, 0, 0, 1},
    {1, 0, 1, 0, 0, 0, 1},
    {1, 0, 1, 1, 1, 1, 1},
    {1, 0, 1, 0, 0, 0, 0},
    {1, 1, 1, 0, 0, 0, 1},
    {1, 1, 1, 1, 1, 1, 1}
  };
  
  get_rectangle_coordinates(tests);
  
  return 0;
}
  
// This code is contributed by mohit kumar 29.


输出:

[[2, 3, 3, 5], [3, 1, 5, 1], [5, 3, 6, 5]]

有关详细信息,请参阅有关查找所有填充为 0 的矩形的完整文章!