0分TLE玄关求调

B4005 [GESP202406 四级] 黑白方块

laoqiu_tyxx @ 2024-11-30 09:52:40

rt

#include<bits/stdc++.h>
using namespace std;
int n,m;
char a[12][12];
bool check(int xa,int xb,int ya,int yb)
{
    int i,j;
    int black=0,white=0;
    for(i=xa;i<=xb;i++)
    {
        for(j=ya;j<=yb;j++)
        {
            if(a[i][j]=='0')
            {
                white++;
            }
            else if(a[i][j]=='1')
            {
                black++;
            }
        }
    }
    return black==white;
}
int main()
{
    int i,j,k,l;
    cin>>n>>m;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    int mx=0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            for(k=i;j<=n;k++)
            {
                for(l=j;l<=m;l++)
                {
                    if(check(i,j,k,l))
                    {
                        mx=max(mx,(k-i+1)*(l-j+1));
                    }
                }
            }
        }
    }
    cout<<mx;
}

by tengyuxuan @ 2024-12-06 20:46:08

@laoqiu_tyxx循环中check(i,j,k,l)改成(i,k,j,l)


by tengyuxuan @ 2024-12-06 20:56:58

@laoqiu_tyxx 改完变RE还是0我也不知道为什么RE,我的代码:

#include <bits/stdc++.h>
using namespace std;
char a[102][102];
bool zx(int stx,int sty,int x,int y){//起始位置与大小
    int bsum=0,hsum=0;//白黑总数
    for(int i=stx;i<=x;i++){
        for(int j=sty;j<=y;j++){
            if(a[i][j]=='0')bsum++;
            if(a[i][j]=='1')hsum++;
        }
    }
    if(bsum==hsum)return 1;
    return 0;
}
int main(){
    int maxn=0;
    bool f=0;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            for(int k=i;k<=n;k++){
                for(int v=j;v<=m;v++){
                    if((k-i+1)*(v-j+1)>maxn&&zx(i,j,k,v)){
                        maxn=(k-i+1)*(v-j+1);
                    }
                }
            }
        }
    }
    cout<<maxn;
    return 0;
}

第2种:

#include<bits/stdc++.h>
using namespace std;
char a[102][102];
bool check(int x1,int x2,int y1,int y2){
    int b[2] = {0,0};
    for(int i = x1;i <= x2;i++){
        for(int j = y1;j <= y2;j++){
            b[(a[i][j]-48)] ++;
        }
    }
    return b[0] == b[1];
}
int main(){
    int n,m;
    cin >> n >> m;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            cin >> a[i][j];
        }
    }
    int ans = 0;
    for(int i = 1;i <= n;i++){
        for(int j= 1;j <= m;j++){
            for(int ii = i;ii <= n;ii++){
                for(int jj = j;jj <= m;jj++){
                    if(check(i,ii,j,jj)&&ans<(ii-i+1) * (jj-j + 1)){
                        ans = (ii-i+1) * (jj-j + 1); 
                    }
                }
            }
        }
    }
    cout << ans;
    return 0;
}

全部AC,放心食用


by laoqiu_tyxx @ 2024-12-11 15:59:46

@tengyu 关了


by zhangjacky @ 2024-12-26 21:16:30

@tengyuxuanme too

#include<bits/stdc++.h>
using namespace std;
int n,m;
char a[12][12];
bool check(int xa,int xb,int ya,int yb)//左上角:(xa,ya)   右上角 (xb,yb);
{
    int i,j;
    //统计黑白 
    int black=0,white=0;
    for(i=xa;i<=xb;i++)
    {
        for(j=ya;j<=yb;j++)
        {
            if(a[i][j]=='0')
            {
                white++;
            }
            else if(a[i][j]=='1')
            {
                black++;
            }
        }
    }
    return black==white;
}
int main()
{
    int i,j,k,l;
    cin>>n>>m;
    //录入矩阵n*m 
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    //最大值 
    int mx=0;
    //左上角(i,j) 
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            //右上角(k,l) 
            for(k=i+1;j<=n;k++)
            {
                for(l=j+1;l<=m;l++)
                {
                    //判断 
                    if(check(i,k,j,l))
                    {
                        //寻找答案 
                        mx=max(mx,(k-i+1)*(l-j+1));
                    }
                }
            }
        }
    }
    //输出 
    cout<<mx;
    return 0; 
}

by zhangjacky @ 2024-12-26 21:18:10

劝你老老实实用前缀和


|