UVA10827-Maximum sum on a torus

Source: Internet
Author: User

UVA10827-Maximum sum on a torus

Question Link


A ring matrix is given, that is, the first row is connected to the last row. The first column is connected to the last column, and the sum of the largest child matrix is obtained.

Idea: as long as four matrices are copied, the sum of the maximum submatrices in a matrix can be calculated, that is, the sum of all submatrices is enumerated and the maximum value is updated. Note that in the converted large matrix, the Child matrix of enumeration has a range.

Code:

#include 
 
  #include 
  
   #include 
   
    #include using namespace std;const int INF = 0x3f3f3f3f;const int MAXN = 205;int arr[MAXN][MAXN], sum[MAXN];int n, Max;int deal() {    int temp;    for (int i = 0; i < n; i++) {          for (int j = 0; j < n; j++) {            memset(sum, 0, sizeof(sum));             for (int k = i; k < i + n; k++) {                temp = 0;                     for (int l = j; l < j + n; l++) {                    sum[l] += arr[k][l];                     temp += sum[l];                    if (Max < temp)                        Max = temp;                }             }         }    }    return Max;}int main() {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%d", &n);         for (int i = 0; i < n; i++) {             for (int j = 0; j < n; j++) {                  scanf("%d", &arr[i][j]);                arr[i][j + n] = arr[i + n][j] = arr[i + n][j + n] = arr[i][j];            }        }        Max = -INF;        int ans = deal();         printf("%d\n", ans);    }    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.