求助!!!悬n关!求在代码上改!

B4005 [GESP202406 四级] 黑白方块

BlackWuKong @ 2024-09-03 13:39:34

#include<bits/stdc++.h>
using namespace std;
int n,m,ans=0; 
struct node{
    char num;
    int qs;
}a[15][15];
int main(){
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cin>>a[i][j].num;
            if (j!=1) a[i][j].qs=a[i][j-1].qs+(a[i][j].num-'0');
            else a[i][j].qs=a[i-1][m].qs+(a[i][j].num-'0');
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            for (int x=1;x<=n;x++){
                for (int y=1;y<=m;y++){
                    if (a[i][j].qs-a[x][y].qs==(m-j+1)*(n-x+1)/2) ans=max(ans,(m-j+1)*(n-x+1));
                }
            }
        }
    } 
    cout<<ans;
    return 0;
}

by Yxy7952 @ 2024-09-03 14:20:04

@lanlingxuan

楼主,看不懂,但我是考过这道题的,只需要4重for循环分别枚举矩形的左上方和右下方两个端点,然后写个判断函数,再用双重for循环检测矩形内黑白格子数量就行了

求关


by BlackWuKong @ 2024-09-03 14:29:55

@Yxy7952 关


by BlackWuKong @ 2024-09-03 14:41:00

@Yxy7952 冒犯的问一下你的代码是咋写的,让我好对比一下自己的代码,求求!


by Yxy7952 @ 2024-09-03 14:43:27

@lanlingxuan

我没交过,稍等我一下


by BlackWuKong @ 2024-09-03 14:44:19

好的好的


by BlackWuKong @ 2024-09-03 14:48:49

@Yxy7952 这是我现在的代码

#include<bits/stdc++.h>
using namespace std;
int n,m,ans=0; 
struct node{
    char num;
    int qs;
}a[15][15];
int main(){
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cin>>a[i][j].num;
            if (j!=1) a[i][j].qs=a[i][j-1].qs+(a[i][j].num-'0');
            else a[i][j].qs=a[i-1][m].qs+(a[i][j].num-'0');
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            for (int x=1;x<=n;x++){
                for (int y=1;y<=m;y++){
                    if (a[i][j].qs-a[x][y].qs==(m-j+1)*x/2&&i!=1) ans=max(ans,(m-j+1)*x);
                    else if (a[i][j].qs-a[x][y].qs==(m-j+1)*x/2) ans=max(ans,(m-j+1)*m);
                }
            }
        }
    } 
    cout<<ans;
    return 0;
}

by Yxy7952 @ 2024-09-03 14:50:15

@lanlingxuan

#include<bits/stdc++.h>
using namespace std;
int n,m,ans=0;
int a[20][20];
bool pd(int x1,int y1,int x2,int y2){
    int h=0,b=0;
    for(int i=x1;i<=x2;i++){
        for(int j=y1;j<=y2;j++){
            if(a[i][j]) h++;
            else b++;
        }
    }
    return h==b;
}
int main(){
    ios::sync_with_stdio(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            char x;
            cin>>x;
            a[i][j]=x-'0';
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            for(int l=i;l<=n;l++){
                for(int r=j;r<=m;r++){
                    if(i==l&&j==r) continue;
                    if(pd(i,j,l,r)){
                        ans=max(ans,(l-i+1)*(r-j+1));
                    }
                }
            }
        }
    }
    cout<<ans;
    return 0;
}
//纯暴力 

by Yxy7952 @ 2024-09-03 14:50:42

@lanlingxuan

简单易懂


by BlackWuKong @ 2024-09-03 14:50:56

@Yxy7952 再次感谢


by BlackWuKong @ 2024-09-03 15:02:13

@Yxy7952 求大神帮忙瞅一眼!

#include<bits/stdc++.h>
using namespace std;
int n,m,ans=0; 
struct node{
    char num;
    int qs;
}a[15][15];
int main(){
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cin>>a[i][j].num;
            if (j!=1) a[i][j].qs=a[i][j-1].qs+(a[i][j].num-'0');
            else a[i][j].qs=a[i-1][m].qs+(a[i][j].num-'0');
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            for (int x=1;x<=n;x++){
                for (int y=1;y<=m;y++){
                    if (a[x][y].qs-a[i][j].qs==(x-i+1)*(y-j+1)/2) ans=max(ans,(x-i+1)*(y-j+1));
                }
            }
        }
    } 
    cout<<ans;
    return 0;
}

| 下一页