uva 10827 - Maximum sum on a torus(最大子矩阵升级版)
来源:互联网 发布:大圣美人进阶数据 编辑:程序博客网 时间:2024/06/06 01:30
1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1768
2、题目大意:
给定一个数字矩阵,求一个最大子矩阵,注意最左边可以和最右边可以作为一个子矩阵,所以需要将原来的矩阵复制成四个矩阵,例如原矩阵为
123
456
789
复制完的矩阵为
123 123
456 456
789 789
123 123
456 456
789 789
然后按照最大子矩阵求,注意控制子矩阵的长度不大于n
2、题目:
Problem H
Maximum sum on a torus
Input: Standard Input
Output: Standard Output
A grid that wraps both horizontally and vertically is called a torus. Given a torus where each cell contains an integer, determine the sub-rectangle with the largest sum. The sum of a sub-rectangle is the sum of all the elements in that rectangle. The grid below shows a torus where the maximum sub-rectangle has been shaded.
1
-1
0
0
-4
2
3
-2
-3
2
4
1
-1
5
0
3
-2
1
-3
2
-3
2
4
1
-4
Input
The first line in the input contains the number of test cases (at most 18). Each case starts with an integer N (1≤N≤75) specifying the size of the torus (always square). Then follows N lines describing the torus, each line containing N integers between -100 and 100, inclusive.
Output
For each test case, output a line containing a single integer: the maximum sum of a sub-rectangle within the torus.
Sample Input Output for Sample Input
2
5
1 -1 0 0 -4
2 3 -2 -3 2
4 1 -1 5 0
3 -2 1 -3 2
-3 2 4 1 -4
3
1 2 3
4 5 6
7 8 9
15
45
3、代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[160][160];int dp[160];int b[160];int maxsum(int *bb,int n){ int maxx=-9999999; int max=-9999999; int i,j; for(j=1; j<=n; j++) { memset(b,0,sizeof(b)); b[j-1]=maxx; for(i=j; i<j+n&&j+n<=2*n; i++) { if(b[i-1]>0) b[i]=b[i-1]+bb[i]; if(b[i-1]<0) b[i]=bb[i]; if(b[i]>max) max=b[i]; } //printf("&&&%d %d %d %d\n",j,i-1,max,b[i-1]); } //printf("\n"); return max;}int main(){ int n,t; scanf("%d",&t); while(t--) { scanf("%d",&n); int max=-99999999; memset(dp,0,sizeof(dp)); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { scanf("%d",&a[i][j]); a[i][j+n]=a[i+n][j]=a[i+n][j+n]=a[i][j]; } }// for(int i=1;i<=n+n;i++)// {// for(int j=1;j<=n+n;j++)// printf("%d ",a[i][j]);// printf("\n");// } for(int i=1; i<=2*n; i++) { memset(dp,0,sizeof(dp)); for(int j=i; j<i+n&&i+n<=2*n; j++) { for(int k=1; k<=2*n; k++) { dp[k]+=a[j][k]; //printf("$$$%d %d %d \n",i,j,dp[k]); } //printf("\n"); int sum=maxsum(dp,n); if(sum>max) max=sum; } memset(dp,0,sizeof(dp)); } printf("%d\n",max); } return 0;}/*251 -1 0 0 -42 3 -2 -3 24 1 -1 5 03 -2 1 -3 2-3 2 4 1 -431 2 34 5 67 8 9*/
- uva 10827 - Maximum sum on a torus(最大子矩阵升级版)
- UVa 10827 Maximum sum on a torus (贪心&矩阵环中的最大子矩阵和)
- UVA - 10827 Maximum sum on a torus 求循环矩阵的最大子矩阵
- UVA 10827 Maximum sum on a torus 可循环的最大连续子矩阵
- UVA - 10827 Maximum sum on a torus(dp最大子矩阵和)
- UVA 10827 Maximum sum on a torus(环面上的最大子矩阵和)
- UVA 10827 Maximum sum on a torus(子矩阵之和变形)
- UVA 10827 Maximum sum on a torus(子矩阵之和变形)(前缀和)
- UVa 10827 - Maximum sum on a torus
- UVa 10827 - Maximum sum on a torus
- uva 10827 - Maximum sum on a torus
- Maximum sum on a torus Uva 10827
- Uva-10827-Maximum sum on a torus
- uva 10827 - Maximum sum on a torus
- UVA - 10827 Maximum sum on a torus
- UVa:10827 Maximum sum on a torus
- UVa 10827 - Maximum sum on a torus
- UVA 10827 - Maximum sum on a torus
- 排序算法之三选择排序
- poj 2406
- android preferenceActivity的用法
- hdu 1164 Eddy's research I
- 编译华天正源码的时候终端会在没有编译完的情况下直接退出的解决方法
- uva 10827 - Maximum sum on a torus(最大子矩阵升级版)
- DetailsView分页显示数据
- 序列化类型为“System.Data.Entity.DynamicProxies.Photos....这个会的对象时检测到循环引用
- 我的革命史,三年来的总结(一)
- 常用正则表达(二)
- HDOJ2546 饭卡(0/1背包)
- 内存对齐
- C++对话系列(一)智能指针 auto_ptr再回忆
- 排序算法之四归并排序