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.
For each test case, output a line containing a single integer: the maximum sum of a sub-rectangle within the torus.
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 |
Problem setter: Jimmy M?rdell
Special Thanks: Derek Kisman, Md. Kamruzzaman
题意:环形矩阵上的最大子矩阵和。
思路:先复制三个矩阵拼接成一个大的矩阵,然后枚举所求最大子矩阵在第一个矩阵中的左上角,再通过动态规划的方法求出长宽不大于N的最大子矩阵,各种枚举情况中的最大和即为所求解。
#include<iostream> #include<cstring> #include<string> using namespace std; int arry[1010][1010]; int main() { string str1,str2; while(getline(cin,str1)) { getline(cin,str2); int i,j,k; memset(arry,0,sizeof(arry)); int len1=str1.size(),len2=str2.size(); for(i=1;i<=len1;i++) { for(j=1;j<=len2;j++) { if(str1[i-1]==str2[j-1]) arry[i][j]=arry[i-1][j-1]+1; else arry[i][j]=max(arry[i-1][j],arry[i][j-1]); } } cout<<arry[len1][len2]<<endl; } return 0; }
[动态规划]UVA10827 - Maximum sum on a torus,布布扣,bubuko.com
[动态规划]UVA10827 - Maximum sum on a torus
原文:http://blog.csdn.net/zju_ziqin/article/details/23603369