Problem:
To find a m*n matrix and the maximum of matrices.
For example, in the following matrix:
0-2-7 0
9 2-6 2
-4 1-4 1
-1 8 0-2
The most Yamato matrices are:
Its and for 15.
Ideas:
First of all, this matrix can be arbitrary size, and the starting point can be anywhere, so, to find out the largest matrix, we have to consider a variety of situations.
Assuming the number of rows for the original matrix is M, for a child matrix, it can have any number of rows from 1 to M, and for a sub-matrix of K < M, its first row can be any row of the original matrix from line 1th to M-k + 1.
Example:
For the above matrix, if the row number of the child matrix is 2, then it can be a matrix of the following matrices:
Or
Or
In each case (we have three of them), we also have to figure out a maximum matrix, which is, of course, the largest matrix of the case (the local maximum), not necessarily the global maximum. But if we know the biggest of every situation, it's a piece of cake to find out the biggest in global.
Before you ask for the largest sub matrix in a particular case, tell the truth first:
Assuming that the dimension of the maximal matrix is one-dimensional, the principle of finding the maximal sub matrix is the same as that of "maximal sub-segment and problem". The recursive formula for the maximal sub segment and the problem is b[j]=max{b[j-1]+a[j], which refers to the largest segment of the A[j]},b[j starting from 0 to J.
Java Implementation Example:
assuming that the original matrix is: [9, 2,-6, 2], then b[] = {9, 11, 5, 7}, then the most large and 11, if the maximum matrix is found, then this matrix is [9, 2]
The code that asks for the largest paragraph and is as follows:
public int maxsubsequence (int[] array) {
if (array.length = 0) {return
0;
}
int max = Integer.min_value;
int[] maxsub = new Int[array.length];
Maxsub[0] = array[0];
for (int i = 1; i < Array.Length i++) {
Maxsub[i] = (Maxsub[i-1] > 0)? (Maxsub[i-1] + array[i]): Array[i];
if (Max < maxsub[i]) {
max = maxsub[i];
}
}
return max;
}
However, the original matrix can be two-dimensional. Assuming that the original matrix is a 3 * n matrix, then its sub matrix can be 1 * k, 2 * k, 3 * k, (1 <= k <= N). If it's 1*k, here are 3 things: the sub-matrix in the first row, the sub-matrix in the second row, and the sub-matrix in the third row. If it is 2 * k, there are two cases where the sub-matrix is in line first to second and the sub-matrix is in line second to third. If it is 3 * k, there is only one case.
In order to be able to find the largest matrix, we need to consider all the cases. Assuming that the matrix is 2 *k, that is, it has only two rows, and to find the maximum matrix, we have to iterate from left to right to find the maximum matrix in this case. If we add the two lines up and down, the situation is the same as asking for the "maximal sub-segment and problem".
In order to find the largest matrix in the original matrix, we have to iterate through all the possible cases of the sub matrices, that is to say, we have to consider that the sub matrix may be only 1 lines, 2 rows, ... to n rows. In each case, we have to add the corresponding matrix part up and down to find the maximum sub matrix (local).
For example, suppose that the child matrix is a 3*k matrix, and that its row is the second line of the original matrix, then we are going to
9 2-6 2
-4 1-4 1
-1 8 0-2
To find the largest matrix.
If we add it up and down, we become 4, 11, -10,1, and from this sequence we can see that in this case the maximum sub matrix is a 3*2 matrix, the largest and the 15.
To be able to quickly get the sum of the upper and lower values from line I to J in the original matrix, we use an auxiliary matrix, which is the original matrix added from top to bottom.
Assuming that the original matrix is the matrix, and that each layer is added up and down to the total, then we can do this by using the following code:
int[][] total = matrix;
for (int i = 1; i < Matrix[0].length. i++) {for
(int j = 0; J < Matrix.length; J +) {
Total[i][j] + = total[ I-1][J];
}
If we ask for the number of values between line I and J, we can get the range from 1 to matrix[0].length-1 by Total[j][k]-total[i-1][k.
With these knowledge points, we only need to compare the local maximal matrix of the corresponding in all cases, we can get the global largest matrix. The code is as follows:
public int Submaxmatrix (int[][] matrix) {
int[][] total = matrix;
for (int i = 1; i < Matrix[0].length. i++) {for
(int j = 0; J < Matrix.length; J +) {
Total[i][j] + = total[ I-1][J];
}
int maximum = Integer.min_value;
for (int i = 0; i < matrix.length. i++) {
for (int j = i; J < Matrix.length; J +) {
//result save from line I to J The row corresponds to the value of the Matrix and
int[] result = new Int[matrix[0].length];
for (int f = 0; f < matrix[0].length; f++) {
if (i = = 0) {
result[f] = total[j][f];
[F] = total[j][f]-total[i-1][f];
}
int maximal = maxsubsequence (result);
if (maximal > maximum) {
maximum = maximal;
}
}} return maximum;
}
C Language-related implementation
Topic
Topic Description:
The size of the known matrix is defined as the and of all elements in the matrix. Given a matrix, your task is to find the largest non-empty (the size is at least 1 * 1) sub matrices.
For example, the matrix of 4 * 4 below
0-2-7 0
9 2-6 2
-4 1-4 1
-1 8 0-2
The maximum sub matrix is
The size of this sub matrix is 15.
Input:
The input is a matrix of n * N. The first line of input gives N (0 < n <= 100).
In the following lines, sequentially (first from left to right gives the n integers of the first row, and then from left to right gives the n integers for the second row ...). Gives a N2 integer in the matrix, separated by a white space character (a space or a blank line).
The range of integers in the known matrix is in [-127, 127].
Output:
There may be multiple sets of test data, and the maximum size of the child matrix is output for each set of test data.
Sample input:
4
0-2-7 0
9 2-6 2
-4 1-4 1
-1 8 0-2
Sample output:
15
AC Code
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int i, J, H, K, N, max, sum, cur, matr IX[101][101];
while (scanf ("%d", &n)!= EOF) {
//Initialize receive matrix for
(i = 0; i < n; i + +) {for
(j = 0; J < N; j + +)
scanf ("%d", * (Matrix + i) + j);
}
Dynamic programming (similar to one dimensional array of consecutive maximal subsequence and)
max = matrix[0][0];
for (i = 0; i < n; i + +) {
//i,j OK up and down bounds for
(j = i; J < N; j + +) {
//Initialize
for (k = i, sum = 0; k <= J; K + +)
sum + = matrix[k][0];
if (Sum > Max)
max = sum;
for (h = 1; h < n; H + +) {
for (k = i, cur = 0; k <= J; k + +)
cur + = matrix[k][h];
if (sum >= 0)
sum + = cur;
else
sum = cur;
if (Sum > max) max = sum;
}
}
printf ("%d\n", max);
}
return 0;
}