uva108 Maximum sum(矩阵最大和)

题目;uva108 Maximum sum(矩阵最大和)

题目大意:给出一个n*n的矩阵,求这个矩阵的最大和。这个矩阵不是连通的,只能在这个矩阵内找子矩阵,不能越过边界。

解题思路:枚举起点和终点,每个起点和终点就是一个矩阵,每个矩阵都算矩阵和,然后保留最大值。每个矩阵的值只要横着相加一遍,再竖着相加一遍,就可以得出以这个矩阵为起点的所有的子矩阵的和(这里可以直接要这个矩阵的和,因为里面的子矩阵枚举起和终点的时候都会算到),然后记录最大值。

例如 : 2 2 3

3 3  4

横着相加  2 4 7

3 6 10

再竖着相加 2 4 7

5  10 17

整个矩阵的和是17

两行一列的和是5

两行两列的和是10

...

只要求以这个起点和终点的矩阵的和就可以了。

代码:

#include <stdio.h>
#include <string.h>

const int N = 150;
const int INF = 0x3f3f3f3f;
int mat[N][N], temx[N], temy[N];

int Max (const int x, const int y) {return x > y? x: y;}

int main () {

	int n;
	while (scanf ("%d", &n) != EOF) {

		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				scanf ("%d", &mat[i][j]);

		int mm = -INF;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++) {

				memset (temx, 0, sizeof (temx));
				for (int x = i; x < n; x++)
					for (int y = j; y < n; y++) {

						if (y == j)
							temy[y] = mat[x][y];
						else
							temy[y] = temy[y - 1] + mat[x][y];
						temx[y] += temy[y];
						mm = Max (mm, temx[y]);
						mm = Max (mm, temy[y]);
					}
			}
		printf ("%d\n", mm);
	}
	return 0;
}

uva108 Maximum sum(矩阵最大和),布布扣,bubuko.com

时间: 2024-12-22 08:37:55

uva108 Maximum sum(矩阵最大和)的相关文章

UVA108 - Maximum Sum(最大连续和)

题意:从一个n*n的矩阵中找出和最大的子矩阵 思路:最大连续和的求解.我们可以将二维的转化为一维进行计算.sum[i][j]表示以(1, 1)和(i, j)为对角的矩阵的和.之后只要逐个枚举每个可能出现的值,保存最大值即可. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3

uva10827-Maximum sum on a torus(矩阵最大和的变形)

题目;uva10827-Maximum sum on a torus(矩阵最大和的变形) 题目大意:就是uva108的变形,矩阵能够连通,就是可以从后面连到前面.这里把矩阵复制三遍,然后重新生成一个大的矩阵,就可以解决联通的问题.再枚举矩阵的起点和终点所有情况,保留最大值就可以了. 例如:1 2 3 2 3 4 新的矩阵: 1 2 3  1 2 3 2 3 4  2 3 4 1 2 3   1 2 3 2 3 4   2 3 4 代码: #include <stdio.h> #include

[LeetCode] 689. Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. Return the result as a list of indices representing the starting p

[Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays

Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M.  (For clarification, the L-length subarray could occur before or after the M-length subarray.) Fo

【UVA】108 - Maximum Sum(DP矩阵)

n^3的复杂度计算最小子矩阵,用了最大连续和的DP算法. 14273282 108 Maximum Sum Accepted C++ 0.013 2014-09-27  #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int INF = 1 << 30; const int maxn = 127 + 3; int mat[maxn][maxn]

[LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个菲重叠子数组的最大和

In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. Return the result as a list of indices representing the starting p

ural 1146 Maximum Sum 最大连续和

1146. Maximum Sum Time limit: 0.5 second Memory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this

1481:Maximum sum

1481:Maximum sum 总时间限制:  1000ms 内存限制:  65536kB 描述 Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: t1 t2 d(A) = max{ ∑ai + ∑aj | 1 <= s1 <= t1 < s2 <= t2 <= n } i=s1 j=s2 Your task is to calculate d(A). 输入

maximum sum

uva,10684 1 #include <iostream> 2 #include <cstdio> 3 #define maxn 10005 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 int a[maxn]; 10 while(scanf("%d",&n),n) 11 { 12 for(int i=0;i<n;i++) 13 scanf("%d",&a[