Poj1050 To the Max maximum submatrix

Source: Internet
Author: User

Given a two-dimenstmarray 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 of all the elements in that rectangle. in this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:

0-2-7 0
9 2-6 2
-4 1-4 1
-1 8 0-2
Is in the lower left corner:

9 2
-4 1
-1 8
And has a sum of 15.

Input

The input consists of an N * N array of integers. the input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimen=array. this is followed by N ^ 2 integers separated by whitespace (spaces and newlines ). these are the N ^ 2 integers of the array, presented in row-major order. that is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. the numbers in the array will be in the range [-127,127].
Output

Output the sum of the maximal sub-rectangle.
Sample Input

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

8 0-2 Sample Output

15

The largest submatrix. First, a row of columns is very simple to find the largest vertex and we want to convert the matrix into a row of columns, that is, to get the sum from top to bottom during input, map [I] [j] indicates the sum of numbers from top to bottom in column J. In this way, a column is converted into a vertex, and then dual, cyclic, the largest sum of the numbers in the first row of column j in any row is the final largest sum.

 

# Include <iostream> # include <stdio. h> using namespace std; int map [105] [105]; int main () {int n, I, j, k, sum, x, max; while (scanf ("% d", & n )! = EOF) {for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) {scanf ("% d ", & x); map [I] [j] = map [I-1] [j] + x;} max =-0x4f4f4f4f; for (I = 0; I <n; I ++) for (j = I; j <n; j ++) {sum = 0; for (k = 0; k <n; k ++) {sum + = map [j] [k]-map [I] [k]; if (sum <0) // if it is smaller than 0, it is equivalent to not needing to be retrieved, remove # include <iostream> # include <stdio. h> using namespace std; int map [105] [105]; int main () {int n, I, j, k, sum, x, max; while (scanf ("% d", & n )! = EOF) {for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) {scanf ("% d ", & x); map [I] [j] = map [I-1] [j] + x;} max =-0x4f4f4f4f; for (I = 0; I <n; I ++) for (j = I; j <n; j ++) {sum = 0; for (k = 0; k <n; k ++) {sum + = map [j] [k]-map [I] [k]; if (sum <0) // if it is smaller than 0, it is equivalent to removing it directly.

Sum = 0;
If (sum> max)
Max = sum;


}
}
Printf ("% d \ n", max );
}

Return 0;
}

Sum = 0;
If (sum> max)
Max = sum;


}
}
Printf ("% d \ n", max );
}

Return 0;
}



 

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.