Uva10827-maximum sum on a torus (maximum and deformation of matrices)
The main idea: is uva108 deformation, the matrix can be connected, is able to connect from the back to the front. Here The matrix is copied three times, and then once again generate a large matrix, you can solve the problem of Unicom. Then enumerate the starting and ending points of the matrix, preserving the maximum value.
For example: 1 2 3
2 3 4
New matrix: 1 2 3 1 2 3
2 3 4 2 3 4
1 2 3 1 2 3
2 3 4 2 3 4
Code:
#include <stdio.h> #include <string.h>const int N = 200;const int INF = 0x3f3f3f3f;int Mat[n][n], temx[n], Temy [N];int n;int Max (const int x, const int y) {return x > y? x:y;} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d", &n), as (int i = 0; i < n; i++) for (int j = 0; j < n; J + +) {scanf ("%d", &mat[i][j]), Mat[i][j + n] = mat[i + N][j] = mat[i + n][j + n] = mat[i][j];} int mm =-inf;for (int i = 0; i < n; i++) {for (int j = 0; J < N; j + +) {memset (temx, 0, sizeof (TEMX)); for (int x = I x < i + N; X + +) {for (int y = j; y < J + N; y++) {if (y = = j) Temy[y] = mat[x][y];elsetemy[y] = temy[y-1] + mat[x][y];temx[y] + = TEMY[Y];MM = max (mm, temy[y]); mm = max (mm, temx[y]);}}} printf ("%d\n", mm);} return 0;}
Uva10827-maximum sum on a torus (Matrix Max and deform)