uva10827-Maximum sum on a torus(矩阵最大和的变形)

题目;uva10827-Maximum sum on a torus(矩阵最大和的变形)


题目大意:就是uva108的变形,矩阵能够连通,就是可以从后面连到前面。这里把矩阵复制三遍,然后重新生成一个大的矩阵,就可以解决联通的问题。再枚举矩阵的起点和终点所有情况,保留最大值就可以了。

例如:1 2 3

       2 3 4

新的矩阵: 1 2 3  1 2 3

                   2 3 4  2 3 4

                   1 2 3   1 2 3

                   2 3 4   2 3 4


代码:

#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);
		for (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];
							else
								temy[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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值