Maximum sum on a torus Uva 10827

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;

const int MAXN(160);
const int INFI((INT_MAX-1) >> 1);
int arr[MAXN][MAXN];
int sum[MAXN][MAXN];

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int n;
		scanf("%d", &n);
		int N = n+n;
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= n; ++j)
			{
				scanf("%d", arr[i]+j);
				arr[i+n][j] = arr[i][j];
				arr[i][j+n] = arr[i][j];
				arr[i+n][j+n] = arr[i][j];
			}
		for(int i = 1; i <= N; ++i)
			for(int j = 1; j <= N; ++j)
				sum[i][j] = sum[i-1][j]+arr[i][j];
		for(int i = 1; i <= N; ++i)
			for(int j = 1; j <= N; ++j)
				sum[i][j] += sum[i][j-1];
		int ans = -INFI;
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= n; ++j)
				for(int k = 1; k <= n; ++k)
					for(int z = 1; z <= n; ++z)
						ans = max(ans, sum[i+k-1][j+z-1]-sum[i+k-1][j-1]-sum[i-1][j+z-1]+sum[i-1][j-1]);
		printf("%d\n", ans);
	}
	return 0;
}

你可能感兴趣的:(Maximum sum on a torus Uva 10827)