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;}