Thứ Sáu, 13 tháng 5, 2022

BONUS C++

#include <bits/stdc++.h>
using namespace std;
int n,k,a[1005][1005],f[1005][1005];

void nhap()
{
    cin>>n>>k;
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=n;j++) cin>>a[i][j];
        
    }
}

void taof()
{
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++) 
            f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+a[i][j];
}

void xuli()
{
    long long maxx=0,s=0;
    int x1,x2,y1,y2;
    for (int x1=1;x1<=n-k+1;x1++)
    {
        for (int y1=1;y1<=n-k+1;y1++)
        {
            x2=x1+k-1;
            y2=y1+k-1;
            s=f[x2][y2]-f[x1-1][y2]-f[x2][y1-1]+f[x1-1][y1-1];
            if (maxx<s) maxx=s;
        }
    }
    cout<<maxx;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    freopen("bonus.inp","r",stdin);
    freopen("bonus.out","w",stdout);
    nhap();
    taof();
    xuli();
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.