稻花香里说丰年,听取蛙声一片

B4005 [GESP202406 四级] 黑白方块

Mr_yang1 @ 2024-09-26 18:07:32

【玄关】

#include<bits/stdc++.h>
using namespace std;
int n,m,a[12][12],maxn=0;
bool can(int x1,int y1,int x2,int y2){
    int a1=0,a0=0;
    for(int i=x1;i<=x2;i++){
        for(int j=y1;j<=x2;j++){
            if(a[i][j])a1++;
            else a0++;
        }
    }
    return a1==a0;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        string s;
        cin>>s;
        for(int j=0;j<m;j++){
            a[i][j+1]=s[j]-'0'+1;
        }
    }
    for(int x1=1;x1<=n;x1++){
        for(int y1=1;y1<=m;y1++){
            for(int x2=1;x2<=n;x2++){
                for(int y2=1;y2<=m;y2++){
                    if(x2<x1||y2<y1)continue;
                    if(can(x1,y1,x2,y2)){
                        maxn=max(maxn,((x2-x1+1)*(y2-y1+1)));
                    }
                }     
            }
        }
    }
    cout<<maxn;
}

by HEzzz @ 2024-09-26 18:52:34

@Mr_yang1

cpp
#include<bits/stdc++.h>
using namespace std;
int n,m,a[12][12],maxn=0;
bool can(int x1,int y1,int x2,int y2){
    int a1=0,a0=0;
    for(int i=x1;i<=x2;i++){
        for(int j=y1;j<=y2;j++){
            if(a[i][j])a1++;
            else a0++;
        }
    }
    return a1==a0;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        string s;
        cin>>s;
        for(int j=0;j<m;j++){
            a[i][j+1]=s[j]-'0';
        }
    }
    for(int x1=1;x1<=n;x1++){
        for(int y1=1;y1<=m;y1++){
            for(int x2=1;x2<=n;x2++){
                for(int y2=1;y2<=m;y2++){
                    if(x2<x1||y2<y1)continue;
                    if(can(x1,y1,x2,y2)){
                        maxn=max(maxn,((x2-x1+1)*(y2-y1+1)));
                    }
                }     
            }
        }
    }
    cout<<maxn;
}

by 210101zhaosicheng @ 2024-09-26 18:54:25

我累个豆啊,D了好久。

can 函数错了

bool can(int x1,int y1,int x2,int y2)
{
    int a1=0,a0=0;
    for(int i=x1;i<=x2;i++){
        for(int j=y1;j<=y2;j++){//不是x2,是y2
            if(a[i][j])a1++;
            else a0++;
        }
    }
    return (a1==a0);
}

by 210101zhaosicheng @ 2024-09-26 18:54:43

@Mr_yang1


|