Submission
Status:
[PPPPPPPPPPPPPPPPPPPPPPPPP]
Subtask/Task Score:
{100/100}
Score: 100
User: chs_14
Problemset: เกาะที่ใหญ่ที่สุด
Language: cpp
Time: 0.003 second
Submitted On: 2026-03-07 21:48:42
#include <bits/stdc++.h>
using namespace std;
pair<int, int> direct[] = {{-1,0},{1,0},{0,-1},{0,1}};
int bfs(int m, int n, int y, int x, vector<string> &sea) {
int counter=1;
queue<pair<int, int>> q;
q.push({y, x});
sea[y][x]='0';
while (!q.empty())
{
pair<int, int> e = q.front(); q.pop();
for (int i = 0; i < 4; i++)
{
int ndy=e.first+direct[i].first, ndx=e.second+direct[i].second;
if (ndy>=0 && ndy<m && ndx>=0 && ndx<n && sea[ndy][ndx]=='1') {
sea[ndy][ndx]='0';
++counter;
q.push({ndy, ndx});
}
}
}
return counter;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m, maxisland=0;
cin >> m >> n;
vector<string> sea(m);
for (int i = 0; i < m; i++)
{
cin >> sea[i];
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (sea[i][j]=='1') maxisland = max(maxisland, bfs(m, n, i, j, sea));
}
}
cout << maxisland;
return 0;
}