Problem description Given a n*m matrix A, a non-empty matrix in a, so that the element in this sub-matrix and the largest.
Wherein, A's sub-matrix refers to a row and column in a continuous block. Input format the first line of input contains two integers n, m, each representing the number of rows and columns of matrix A.
The next n rows, m integers per line, represent matrix A. The output format outputs a row that contains an integer representing the elements in the largest sub-matrix in a. Sample Input 3 3
-1-4 3
3 4-1
-5-2 8 Sample Output 10 Sample Description take the last column, and 10. Data size and conventions for 50% of data, 1<=n, m<=50;
For 100% of data, 1<=n, the absolute value of each element in M<=500,a does not exceed 5000. Idea: Save the and (bottom-up) of each row i~j column with b[n+1], then the maximum subsequence and, last update maxsum
#include <stdio.h>#include<stdlib.h>#include<string.h>#defineN 501intA[n][n];intn,m;intSub_sum (int*b) { intS,max; S= max = b[1]; inti; for(i=2; i<=n;i++) {s+=B[i]; if(S >max) Max=s; if(S <0) s=0; } returnMax;}intMain () {inti,j,t,maxsum,s; int*b; scanf ("%d%d",&n,&m); b= (int*)malloc(sizeof(int) * (n+1)); for(i=1; i<=n;i++) for(j=1; j<=m;j++) {scanf ("%d",&A[i][j]); } maxsum= a[1][1]; for(i=1; i<=m;i++) {memset (b,0, (n+1)*sizeof(int)); for(j=i;j<=m;j++) { //Fixed i,j column for(t=1; t<=n;t++) B[t]+ = A[t][j];//bottom-up (fixed column m,m,n (fixed row n,n,m))s =Sub_sum (b); if(S >maxsum) Maxsum=s; }} printf ("%d\n", maxsum); return 0;}
Maximum sub-array of previous test questions