2 solutions

  • 1
    @ 2026-4-1 11:33:29
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1010;
    char a[N][N];
    int x[N][N];
    int dx[5]={0,0,-1,1,0};
    int dy[5]={-1,1,0,0,0};
    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 res=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                int cnt=0;
                for(int k=0;k<4;k++)
                {
                    if(a[i+dx[k]][j+dy[k]]=='#') cnt++;
                }
                if(a[i][j]=='.'&&cnt==0)
                {
                    res++;
                }
                x[i][j]=cnt;
            }
        }
        int maxv=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]=='.')
                {
                    continue;
                }
                else
                {
                    int c=0;
                    for(int k=0;k<4;k++)
                    {
                        int xx=dx[k]+i,yy=dy[k]+j;
                        if(x[xx][yy]==1&&a[xx][yy]=='.')
                        {
                            c++;
                        }
                    }
                    if(a[i][j]=='#'&&x[i][j]==0) c++;
                    maxv=max(maxv,c);
                }
            }
        }
        cout<<maxv+res;
        return 0;
    }
    

    Information

    ID
    2565
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    16
    Accepted
    2
    Uploaded By