TOJ 4475: The Coolest Sub-matrix

4475: The Coolest Sub-matrix 分享至QQ空间

Time Limit(Common/Java):4000MS/12000MS     Memory Limit:65536KByte
Total Submit: 50            Accepted:13

Description

 

Given an N*N matrix, find the coolest square sub-matrix.
We define the cool value of the square matrix as X-Y where X indicating the sum of all integers of the main diagonal and Y indicating the sum of the other diagonal.

 

Input

 

The first line has a positive integer N (2 ≤ N ≤ 400), the size of the matrix.
The following N lines each contain N integers in the range [-1000, 1000], the elements of the matrix.

 

Output

 

Output the coolest value of a square sub-matrix.

 

Sample Input

2
1 -2
4 5

Sample Output

 4

Source

TOJ

就是三重循环啊,大家怎么都不做,记得当时是我想错这个题了,导致当时我们队没有过这个题

就是让你随意在这个矩形选一个正方形矩阵,计算主对角线和副对角线的差值的最大值

只能枚举了,前缀和处理下,这个题只需要注意下标不一溢出就行了,思路还是很简单的,第i行j列结尾的k*k矩阵

 

#include <stdio.h>
#include <algorithm>
using namespace std;
int a[405][405],b[405][405];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++){
        int x;
        scanf("%d",&x);
        a[i][j]=x+a[i-1][j-1];b[i][j]=x+b[i-1][j+1];
    }
    int ma=0;
    for(int i=2;i<=n;i++)
    for(int j=2;j<=n;j++)
    for(int k=2;k<=i&&k<=j;k++)
    ma=max(ma,a[i][j]-a[i-k][j-k]-b[i][j-k+1]+b[i-k][j+1]);
    printf("%d\n",ma);
    return 0;
}

 

 

 

转载于:https://www.cnblogs.com/BobHuang/p/7273317.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值