求助,实在找不出来哪里bug了。。。。为啥不能输出呢。。

P3397 地毯

_Hal @ 2023-07-28 10:57:59

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 1010;
int a[N][N], b[N][N];
void ist(int x1, int y1, int x2, int y2, int x)
{
    b[x1][y1] += x;
    b[x2 + 1][y1] -= x;
    b[x1][y2 + 1] -= x;
    b[x2 + 1][y2 + 1] += x;
}

int main()
{
    int n, m, i, j;
    scanf("%d%d", &n, &m);
    for (i = 1; i <= n; i++)    // yes
    {
        for (j = 1; j <= n; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }   
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            ist(i, j, i, j, a[i][j]);
        }
    }
    int x1, y1, x2, y2, x;
    for (i = 1; i <= m; i++)
    {
        //puts("Yes");
        scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &x);
        ist(x1, y1, x2, y2, x);
    }
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            a[i][j] = a[i][j - 1] + a[i - 1][j] - a[i - 1][j - 1] + b[i][j];
        }
    }
    for (i = 1; i <= n; i++)
    {   
        for (j = 1; j <= n; j++)
        {
            printf("%d ", a[i][j]);
        }
        putchar('\n');
    }

    system("pause");

    return 0;
}

by PVZ__2 @ 2023-07-28 11:28:27

@ln2____

#include<bits/stdc++.h>
using namespace std;
int t[1005][1005];
int main(){
    int n,m,x1,x2,y1,y2;
    cin>>n>>m;
    for (int k=1;k<=m;k++){
        cin>>x1>>y1>>x2>>y2;
        for (int i=x1;i<=x2;i++){
            for (int j=y1;j<=y2;j++)t[i][j]++;
        }   
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++)cout<<t[i][j]<<' ';
        cout<<endl;
    }
    return 0;
}

个人刚写的代码,AC了,求关呗


by PVZ__2 @ 2023-07-28 11:31:01

用前缀和做


by wangderui111 @ 2024-05-30 21:04:11

兄弟,恕我直言,你的输入多了个X


by wangderui111 @ 2024-05-30 21:05:37

scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &x);

输入应该是

scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

在系统的判定中,你还没有输入完,就不会执行下面的操作


by wangderui111 @ 2024-05-30 21:10:35

整个代码都有问题,个人建议重写,改比重写还难


by wangderui111 @ 2024-05-30 21:11:20

实在不行就看一下我的暴力算法

#include<bits/stdc++.h>
using namespace std;
int ans[1005][1005];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        for(int x=x1;x<=x2;x++){
            for(int y=y1;y<=y2;y++){
                ans[x][y]++;
            }
        }
    }for(int x=1;x<=n;x++){
        for(int y=1;y<=n;y++){
            cout<<ans[x][y]<<" ";
        }cout<<endl;
    }return 0;
}

by wangderui111 @ 2024-05-30 21:11:47

@_Hal


by wangderui111 @ 2024-05-30 21:11:56

@_Hal


by wangderui111 @ 2024-05-30 21:12:38

@_Hal

@_Hal

@_Hal

@_Hal

@_Hal

@_Hal

@_Hal

@_Hal

@_Hal

@_Hal

@_Hal

@_Hal

@_Hal


by wangderui111 @ 2024-05-30 21:13:10

@_Hal

看下评论区


|