Ultraviolet A 108: Maximum Sum

Source: Internet
Author: User

This question is solved by brute force + dynamic planning. The analysis is as follows:

For a 1 * m matrix, that is, a series, maximal sub-rectangle can be obtained by finding the maximum long continuous string and dynamic programming ).

Then, for the n * m matrix, sum the numbers in each column to obtain a 1 * m matrix, the greatest sum obtained by using the method described above is the maximum submatrix sum in the submatrix where the number of all rows of the n * m matrix is n.

Then this question is enumerated by enumerating all rows as 1, 2, 3 ..... N matrix (brute force), respectively use the above method to compress the Matrix to obtain the maximum continuous string and find the maximum value, that is, the result.

My problem-solving code is as follows:

 

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <string>#include <algorithm>using namespace std;int table[100][100];int sum[100];int N;int max_continuous_sum(){int maxs=0,s=0;for(int i=0; i<N; i++){if(s>=0) s+=sum[i];else s=sum[i];maxs = maxs>s ? maxs : s;}return maxs;}int main(){cin >> N;int maxsum=0;int tmp;for(int i=0; i<N; i++){for(int j=0; j<N; j++){cin >> table[i][j];sum[j]=table[i][j];}tmp = max_continuous_sum();maxsum = maxsum>tmp ? maxsum : tmp;for(int j=i-1; j>=0; j--){for(int k=0; k<N; k++)sum[k]+=table[j][k];tmp = max_continuous_sum();maxsum = maxsum>tmp ? maxsum : tmp;}}cout << maxsum << endl;return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.