c_cpp 数组中的max_sum矩形
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 数组中的max_sum矩形相关的知识,希望对你有一定的参考价值。
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
int kadane(int* arr, int* start, int* finish, int n){
int sum = 0, maxSum = INT_MIN, i;
*finish = -1;
int local_start = 0;
for(int i = 0;i < n;i++){
sum += arr[i];
if(sum < 0){
sum = 0;
local_start = i+1;
}
if(sum > maxSum){
maxSum = sum;
*start = local_start;
*finish = i;
}
}
return maxSum;
}
void findMaxSum(int M[][COL]){
int maxSum = INT_MIN, finalLeft, finalRight, finalTop, finalBottom;
int left, right, i;
int temp[ROW], sum, start, finish;
for(left = 0;left < COL;++left){
memset(temp,0,sizeof(temp));
for (right = left; right < COL; ++right)
{
for (i = 0; i < ROW; ++i){
temp[i] += M[i][right];
}
sum = kadane(temp, &start, &finish, ROW);
if (sum > maxSum)
{
maxSum = sum;
finalLeft = left;
finalRight = right;
finalTop = start;
finalBottom = finish;
}
}
}
cout << finalLeft << " "<< finalRight << endl;
cout << finalTop << " " <<finalBottom << endl;
cout <<maxSum << endl;
}
int main() {
int M[ROW][COL] = {{1, 2, -1, -4, -20},
{-8, -3, 4, 2, 1},
{3, 8, 10, 1, 3},
{-4, -1, 1, 7, -6}
};
findMaxSum(M);
return 0;
}
以上是关于c_cpp 数组中的max_sum矩形的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 使用VAO(顶点数组对象)和顶点缓冲区定义和绘制矩形。
c_cpp 矩形区域
c_cpp 矩形覆盖的.cpp
c_cpp 【73】创建包围轮廓的矩形边界
c_cpp 直方图中最大的矩形区域
c_cpp 摄像头物体跟踪定位(应用最小矩形)