在C ++中,最大子矩阵区域的计数为1的点数大于0的计数
在本教程中,我们将讨论一个程序,以找到最大子矩阵区域,该子矩阵区域的计数为1的点数大于0的点数。
为此,我们将提供一个包含0和1的矩阵。我们的任务是获取包含大于1的1的最大面积的子矩阵
示例
#include <bits/stdc++.h> using namespace std; #define SIZE 10 //查找最长子矩阵的长度 int lenOfLongSubarr(int arr[], int n, int& start, int& finish) { unordered_map<int, int> um; int sum = 0, maxLen = 0; for (int i = 0; i < n; i++) { sum += arr[i]; if (sum == 1) { start = 0; finish = i; maxLen = i + 1; } else if (um.find(sum) == um.end()) um[sum] = i; if (um.find(sum - 1) != um.end()) { if (maxLen < (i - um[sum - 1])) start = um[sum - 1] + 1; finish = i; maxLen = i - um[sum - 1]; } } return maxLen; } //找到最大面积 void largestSubmatrix(int mat[SIZE][SIZE], int n) { int finalLeft, finalRight, finalTop, finalBottom; int temp[n], maxArea = 0, len, start, finish; for (int left = 0; left < n; left++) { memset(temp, 0, sizeof(temp)); for (int right = left; right < n; right++) { for (int i = 0; i < n; ++i) temp[i] += mat[i][right] == 0 ? -1 : 1; len = lenOfLongSubarr(temp, n, start, finish); if ((len != 0) && (maxArea < (finish - start + 1) * (right - left + 1))) { finalLeft = left; finalRight = right; finalTop = start; finalBottom = finish; maxArea = (finish - start + 1) * (right - left + 1); } } } cout << "(Top, Left): (" << finalTop << ", " << finalLeft << ")\n"; cout << "(Bottom, Right): (" << finalBottom << ", " << finalRight << ")\n"; cout << "Maximum area: " << maxArea; } int main() { int mat[SIZE][SIZE] = { { 1, 0, 0, 1 }, { 0, 1, 1, 1 }, { 1, 0, 0, 0 }, { 0, 1, 0, 1 } }; int n = 4; largestSubmatrix(mat, n); return 0; }
输出结果
(Top, Left): (1, 1) (Bottom, Right): (3, 3) Maximum area: 9