Submission

Status:
[PPPPPPPPPPPPPPPPPPPPPPPPP]

Score: 100

User: FotoFatTurtle

Problemset: เกาะที่ใหญ่ที่สุด

Language: cpp

Time: 0.003 second

Submitted On: 2025-04-01 20:44:06

#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,m;
    cin>>n>>m;
    char mp[n][m];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>mp[i][j];
        }
    }
    queue<pair<int,int>> q;
    int i,j,nub,ans=0;
    for(int x=0;x<n;x++)
    {
        for(int y=0;y<m;y++)
        {
            if(mp[x][y]=='1')
            {
                mp[x][y]='0';
                //cout<<"\n";
                q.push({x,y});
                nub=0;
                while(!q.empty())
                {
                    i=q.front().f;
                    j=q.front().s;
                    q.pop();
                    //cout<<x<<" "<<y<<"\n";
                    nub++;
                    if(i>0&&mp[i-1][j]=='1')
                    {
                        mp[i-1][j]='0';
                        q.push({i-1,j});
                    }
                    if(j>0&&mp[i][j-1]=='1')
                    {
                        mp[i][j-1]='0';
                        q.push({i,j-1});
                    }
                    if(i<n-1&&mp[i+1][j]=='1')
                    {
                        mp[i+1][j]='0';
                        q.push({i+1,j});
                    }
                    if(j<m-1&&mp[i][j+1]=='1')
                    {
                        mp[i][j+1]='0';
                        q.push({i,j+1});
                    }
                }
                ans=max(ans,nub);
            }
        }
    }
    cout<<ans;
}