上一篇环状二维数组还存在问题,当时并未判断产生的子矩阵是否超过矩阵的范围,所以结果会出现问题。
改进版的程序中对此进行了判断。
此问题分为两种,一种是最大子数组成环,一种是最大子数组未成环。未成环的部分之前已实现,下面是成环部分。
开始的思路是:将矩阵进行扩大,类似于一维数组,但是扩大后就会出现子矩阵的范围有可能超出矩阵的范围。所以在此处要有一个判断。
改进后的程序
#include <iostream> #include<time.h> using namespace std; #define max(a,b) ((a)>(b)?(a):(b)) #define MAXN 100 int A[MAXN][MAXN]; int PartSum[MAXN][MAXN]; //计算子矩阵的和 int MatrixSum(int s, int t, int i, int j) { return PartSum[i][j] - PartSum[i][t - 1] - PartSum[s - 1][j] + PartSum[s - 1][t - 1]; } int main() { srand((unsigned)time(NULL)); int row, col, i, j; cout << "请输入二维数组的行数和列数:"; cin >> row >> col; for (i = 1; i <= row; i++) { for (j = 1; j <= 2 * col - 1; j++) { for (j = 1; j <=col; j++) { A[i][j] = rand() % 20 - 10; cout << A[i][j] << " \t"; } for (j = col+1; j <= 2 * col - 1; j++) { A[i][j] = A[i][j - col]; cout << A[i][j] << " \t"; } //cout << A[i][j] << " "; } cout<<endl; } for (i = 0; i <= row; i++) PartSum[i][0] = 0; for (j = 0; j <=2* col-1; j++) PartSum[0][j] = 0; // 计算矩阵的部分和 for (i = 1; i <= row; i++) for (j = 1; j <= col; j++) PartSum[i][j] = A[i][j] + PartSum[i - 1][j] + PartSum[i][j - 1] - PartSum[i - 1][j - 1]; int n1, n2; int maxsofar = A[1][1]; for (n1 = 1; n1 <= row; n1++) for (n2 = n1; n2 <= row; n2++) { // 将子矩阵上下边界设为第n1行和第n2行,在这些子矩阵中取最大值,类似于一维数组求最大值 //未成环 int maxendinghere = MatrixSum(n1, 1, n2, 1); for (j = 2; j <= col-1; j++) { maxendinghere = max(MatrixSum(n1, j, n2, j), MatrixSum(n1, j, n2, j) + maxendinghere); maxsofar = max(maxendinghere, maxsofar); } //成环 int sum = MatrixSum(n1, 1, n2, 1); int start = sum; int sind = 1; for (i = 2; i <=row; i++) { sum += MatrixSum(n1, i, n2, i); if (sum > start) { start = sum; sind = i; } } maxendinghere = MatrixSum(n1, j, n2, j); int tind = row; for (j = row - 1; j >= 1; j--) { sum += MatrixSum(n1, j, n2, j); if (sum > maxendinghere) { maxendinghere = sum; tind = j; } } if (sind<tind && start + maxendinghere>maxsofar) { maxsofar = start + maxendinghere; } } cout << maxsofar; }
时间: 2024-10-09 21:16:24