Submission

Status:

[PPP-SSSSSSSSSSSSSSSSSSSSS]

Subtask/Task Score:

{0/100}

Score: 0

User: Shangbin

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

Language: cpp

Time: 0.003 second

Submitted On: 2026-01-24 23:06:07

//Problem = https://grader.gchan.moe/problemset/c2_st66_largest_island
#include <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>

const int maxn = 175;
int m, n, visited[maxn][maxn], islandsize, dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}, biggest = -1e9;
char mat[maxn][maxn];
queue<pii> q;

int main(){
    cin.tie(NULL)->sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            cin >> mat[i][j];
        }
    }
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            if (mat[i][j] == '1' && !visited[i][j]){
                q.push({i, j});
                islandsize = 0;
                while (!q.empty()){
                    auto [qx, qy] = q.front(); q.pop();
                    if (visited[qx][qy]) continue;
                    visited[qx][qy] = 1;
                    islandsize++;
                    for (auto [x, y] : dir){
                        int nx = qx + x, ny = qy + y;
                        if (mat[nx][ny] == '1'){
                            q.push({nx, ny});
                        }
                    }
                }
                biggest = max(biggest, islandsize);
            }
        }
    }
    //for (auto i : island) cout << i << ' ';
    cout << biggest;
}