Chapter 1 of "algorithm competition-Training Guide"-1.24 _ minimum continuous matrix and

Source: Internet
Author: User

Expanding from a one-dimensional array to a two-dimensional matrix is a wonderful process ......

The method of the Two-dimensional matrix is to use the method of the one-dimensional array, that is, to enumerate any two rows, and then find the sum of elements in the same column between the two rows, and then use the method of the one-dimensional array, the maximum continuous sum is the maximum submatrix sum of the current enumeration.

Of course, the idea is to think about how to implement it. It is also a good implementation, that is, I have been confused by the sum [x] [Y] mentioned above, and I have never known how to use it, I also thought about how to use it, but it is useless to use it. The time efficiency is almost the same as that obtained by enumeration.

Paste the Code:

# Include <stdio. h> # include <string. h >#include <iostream >#include <string> using namespace STD; const int maxn = 10; const int INF = 1 <30; int N; int m; int A [maxn] [maxn]; int s [maxn] [maxn]; int C [maxn]; int T [maxn]; int maxsum (int n, int * B) {for (INT I = 1; I <= N; I ++) {T [I] = B [I]-B [I-1];} int ans = 0; int sum = T [1]; for (INT I = 2; I <= N; I ++) {If (sum> 0) {sum + = T [I];} else {sum = T [I];} If (ANS <sum) {Ans = sum ;}} return ans;} int main () {While (scanf ("% d", & N, & M )! = EOF) {memset (S, 0, sizeof (s); For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= m; j ++) {scanf ("% d", & A [I] [J]) ;}} for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= m; j ++) {s [I] [J] = s [I-1] [J] + s [I] [J-1]-s [I-1] [J-1] + A [I] [J] ;}} int sum =-INF; For (INT I = 1; I <= N; I ++) {for (Int J = I; j <= N; j ++) {c [0] = 0; For (int K = 1; k <= m; k ++) {c [k] = s [J] [k]-s [I-1] [k];} int ans = maxsum (M, C ); if (ANS> sum) {sum = ans ;}}/* For (INT I = 1; I <= N; I ++) {memset (C, 0, sizeof (c); For (Int J = I; j <= N; j ++) {C [0] = 0; For (int K = 1; k <= m; k ++) {C [k] + = A [J] [k];} int ans = maxsum (M, C ); if (ANS> sum) {sum = ans ;}}// this method can also be equivalent to the sum when enumeration columns are obtained. */printf ("% d \ n", sum);} system ("pause"); 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.