最大和 -- 最大子矩阵

从一维的最大子序列 , 到二维的最大子序列 , 实际上还是转化为一维的去计算 , 通过输入时获得操作 , 和下面的 三个 for 循环 得以 计算最大子序列 /

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<vector>
 8 #include<set>
 9 #include<stack>
10 #include<string>
11 #include<sstream>
12 #include<map>
13 #include<cctype>
14 #include<limits.h>
15 using namespace std;
16 int main()
17 {
18     int t,a[2][6],n,m;
19     scanf("%d",&t);
20     while(t--)
21     {
22         scanf("%d%d",&n,&m);
23         memset(a,0,sizeof(a));
24         for(int i=1;i<=n;i++)
25             for(int j=1;j<=m;j++)
26             {
27                 scanf("%d",&a[i][j]);
28                 a[i][j]=a[i][j]+a[i-1][j]; //   这里是一个重要的处理 ,  让下面的数据 , 代表该数据及以上数据之和  便于下方计算
29             }
30         int temp,result=INT_MIN;
31         for(int i=1;i<=n;i++)     //            通过这三个  for 循环 得以计算到  最大值的最小子序列 .
32             for(int j=i;j<=n;j++)
33             for(int k=1,maxn=INT_MIN;k<=m;k++)
34         {
35             temp=a[j][k]-a[i-1][k];
36             maxn=(maxn>0?maxn:0)+temp;
37             result=maxn>result?maxn:result;
38         }
39         printf("%d\n",result);
40     }
41     return 0;
42 }
时间: 2024-08-02 09:26:13

最大和 -- 最大子矩阵的相关文章

hdu 1081 &amp; poj 1050 To The Max(最大和的子矩阵)

转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum

nyoj 104——最大和——————【子矩阵最大和】

最大和 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 给定一个由整数组成二维矩阵(r*c),现在需要找出它的一个子矩阵,使得这个子矩阵内的所有元素之和最大,并把这个子矩阵称为最大子矩阵. 例子:0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2 其最大子矩阵为: 9 2 -4 1 -1 8 其元素总和为15. 输入 第一行输入一个整数n(0<n<=100),表示有n组测试数据:每组测试数据:第一行有两个的整数r,c(0<r,c&l

hdu 1081 &amp;amp; poj 1050 To The Max(最大和的子矩阵)

转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum

代码题(35)— 最大和子矩阵

1.最大和子矩阵 问题:求一个M*N的矩阵的最大子矩阵和.比如在如下这个矩阵中:0 -2 -7 09 2 -6 2-4 1 -4 1-1 8 0 -2 拥有最大和的子矩阵为:9 2-4 1-1 8其和为15. 假定原始矩阵的行数为M,那么对于子矩阵,它的行数可以是1到M的任何一个数,而且,对于一个K行(K < M)的子矩阵,它的第一行可以是原始矩阵的第1行到 M - K + 1 的任意一行.同时对于每一个得到的子矩阵,假设这个子矩阵是 2 *k, 也就是说它只有两行,要找出最大子矩阵,我们要从左

最大子矩阵,最大连续子数组进阶,动态规划初级,poj1050

题目描述:现给出一个N*N矩阵,要求求出拥有最大和的子矩阵的和. 例如: 这样的一个矩阵,最大子矩阵的和为15: 此题可以让人联想到求最大连续子数组,求最大子数组在上一篇文章中http://www.cnblogs.com/tz346125264/p/7560708.html. 分析:最大子矩阵可以看为求最大连续子数组拓展到二维数组上,因为矩阵的性质同样在横向竖向上需要连续,那么可以想办法将这个二维数组简化为求连续子数组. 思考: 1.要求最大子矩阵,必须保证每个矩阵都被浏览到,为了保证运行时间尽

结对开发——求二维环形数组所有子矩阵最大和的问题

一.题目要求: 输入一个二维整形数组,数组里有正数也有负数. 二维数组首尾相接,象个一条首尾相接带子一样. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值. 要求时间复杂度为O(n)题目:返回一个二维整数数组中最大子数组的和. 二.解决思路: 由于上次我们做过求二维数组最大子矩阵和的问题,又做了求一维环状数组的子数组最大值问题,这次就在以前的基础上进行修改,先对二维数组进行了重构,形成一个环状二维数组,然后再用求二维数组子矩阵最大和的方法求得最终结果.

HDU 1559 最大子矩阵--dp--(最大和子矩阵模型)

题意:给定一个矩阵,求一个宽为a,长为b的子矩阵的最大和 分析:直接用最大和子矩阵的公式,只是这里多了一个限制条件,所以在求和的时候稍有点不同,就是不逐行和逐列求了而是每次以a,b为跨度来求 dp[k]=mt[i][k]+....+mt[i+a][k],然后每次求sum=dp[k]+.....+dp[k+b],mx=max(mx,sum) 代码: #include<iostream> #include<cstring> using namespace std; int m,n,a,

一个矩阵的所有子矩阵最大和问题

Preface ??今天早上刷微博,看到LeetCode中国微博发了这样一条状态: ??已经好久没做题练练手了,于是想试试.LeetCode上,该题目的地址为:https://leetcode.com/problems/max-sum-of-sub-matrix-no-larger-than-k/ Analysis ??想了一上午,也没想出什么头绪.后来我看 LeetCode 上有不少人已经做出提交了.并且,在discuss页面里,有人公布了详细的解释与代码. ??我看了一下,他这个解法是基于K

ZOJ1074 (最大和子矩阵 DP)

F - 最大子矩阵和 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the