Openjudge 2766 maximum submatrix

Source: Internet
Author: User

1. Link:

Http://bailian.openjudge.cn/practice/2766

2. Question:

Total time limit:
1000 ms
Memory limit:
65536kb
Description
The size of the known matrix is defined as the sum of all elements in the matrix. Given a matrix, your task is to find the largest non-null (size at least 1*1) submatrix.

For example, the following 4*4 Matrix

0-2-7 0
9 2-6 2
-4 1-4 1
-1 8 0-2

Is

9 2
-4 1
-1 8

The size of this Sub-matrix is 15.
Input
The input is a matrix of N * n. The first line of the input is n (0 <n <= 100 ). The following rows are in sequence (first, N Integers of the first row are given from left to right, and then n Integers of the second row are given from left to right ......) The N2 integers in the matrix are given. integers are separated by spaces (spaces or empty rows ). The range of integers in the known matrix is [-127,127].
Output
The maximum size of the output sub-matrix.
Sample Input
40 -2 -7 0 9 2 -6 2-4 1 -4  1 -18  0 -2
Sample output
15
Source
Translated from greater New York 2001

3. Ideas:

The largest expanded field and. Traverse all possible rows first K = 1-N. Then calculate the sum of each column in the K-row matrix and convert it to one-dimensional. The maximum value is obtained using the maximum field sum method.

4. Code:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4  5 using namespace std; 6  7 int main() 8 { 9     //freopen("C://input.txt","r",stdin);10 11     int i,j,k;12 13     int n;14     cin >> n;15 16     int **arr_matrix = new int*[n];17     for(i = 0; i < n; ++i) arr_matrix[i] = new int[n];18 19 20     for(i = 0;i < n; ++i)21     {22         for(j = 0;j < n; ++j)23         {24             cin >> arr_matrix[i][j];25         }26     }27 28     int *arr_temp = new int[n];29 30     int *dp = new int[n];31 32     int max_sum = arr_matrix[0][0];33     for(k = 0; k < n; ++k)34     {35         memset(arr_temp,0,sizeof(int) * n);36         for(j = 0; j < n; ++j)37         {38             for(i = 0; i < k; ++i) arr_temp[j] += arr_matrix[i][j];39         }40 41         for(i = k; i < n; ++i)42         {43             for(j = 0; j < n; ++j) arr_temp[j] += arr_matrix[i][j];44 45             memset(dp,0,sizeof(int) * n);46             dp[0] = arr_temp[0];47             for(j = 1; j < n; ++j)48             {49                 dp[j] = ((dp[j - 1] + arr_temp[j]) > arr_temp[j]) ? (dp[j - 1] + arr_temp[j]) : arr_temp[j];50                 if(max_sum < dp[j]) max_sum = dp[j];51             }52 53             for(j = 0; j < n; ++j) arr_temp[j] -= arr_matrix[i - k][j];54         }55     }56 57     cout << max_sum << endl;58 59     delete [] dp;60 61     delete [] arr_temp;62 63     for(i = 0; i < n; ++i) delete [] arr_matrix[i];64     delete [] arr_matrix;65 66     return 0;67 }

 

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.