Submission #74293394


Source Code Expand

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include<queue>

using namespace std;

vector<int> dx = {0, 0, -1, 1};
vector<int> dy = {1, -1, 0, 0};
void solve() {
    
    int n, m; cin >> n >> m;

    vector<string> grid(n);

    for(auto &i: grid){
        cin >> i;
    }

    queue<pair<int, int>> q;

    for(int i = 0; i < n; i++){
        if(grid[i][0] == '.'){
            q.push({i, 0});
            grid[i][0] = '#';
        }

        if(grid[i][m-1] == '.'){
            q.push({i, m - 1});
            grid[i][m-1] = '#';
        }
    }

    for(int j = 0; j < m; j++){
        if(grid[0][j] == '.'){
            q.push({0, j});
            grid[0][j] = '#';
        }
        if(grid[n-1][j] == '.'){
            q.push({n-1, j});
            grid[n-1][j] = '#';
        }
        
    }

    while(!q.empty()){
        pair<int, int> u = q.front();
        q.pop();

        for(int d = 0; d < 4; d++){
            int nx = u.first + dx[d];
            int ny = u.second + dy[d];

            if(nx >= n || nx < 0 || ny >= m || ny < 0) continue;

            if(grid[nx][ny] == '#') continue;

            q.push({nx, ny});
            grid[nx][ny] = '#';
        }
    }

    auto dfs = [&](int x, int y, auto && self) -> void {
        
        grid[x][y] = '#';
        for(int d = 0; d < 4; d++){
            int nx = x + dx[d];
            int ny = y + dy[d];

            if(nx >= n || nx < 0 || ny >= m || ny < 0) continue;

            if(grid[nx][ny] == '#') continue;

            self(nx, ny, self);
        }
    };

    int ans = 0;
    for(int i = 1; i < n-1; i++){
        for(int j = 1; j < m - 1; j++){
            if(grid[i][j] == '#') continue;
            dfs(i, j, dfs);
            ans++;
        }
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int tc = 1;
    // cin >> tc;
    while(tc--){
        solve();
    }
    return 0;
}

Submission Info

Submission Time
Task C - Puddles
User gkishor03
Language C++23 (GCC 15.2.0)
Score 300
Code Size 2062 Byte
Status AC
Exec Time 19 ms
Memory 16388 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 2
AC × 24
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All min.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
min.txt AC 1 ms 3680 KiB
random_01.txt AC 7 ms 4796 KiB
random_02.txt AC 3 ms 3880 KiB
random_03.txt AC 7 ms 4748 KiB
random_04.txt AC 4 ms 4188 KiB
random_05.txt AC 11 ms 4832 KiB
random_06.txt AC 1 ms 3584 KiB
random_07.txt AC 11 ms 4740 KiB
random_08.txt AC 1 ms 3736 KiB
random_09.txt AC 16 ms 4748 KiB
random_10.txt AC 12 ms 4488 KiB
random_11.txt AC 16 ms 4744 KiB
random_12.txt AC 1 ms 3456 KiB
random_13.txt AC 19 ms 4808 KiB
random_14.txt AC 2 ms 3768 KiB
random_15.txt AC 19 ms 4604 KiB
random_16.txt AC 7 ms 4028 KiB
random_17.txt AC 2 ms 4760 KiB
random_18.txt AC 13 ms 4800 KiB
random_19.txt AC 5 ms 4672 KiB
random_20.txt AC 11 ms 16388 KiB
random_21.txt AC 8 ms 4744 KiB
sample_01.txt AC 1 ms 3456 KiB
sample_02.txt AC 1 ms 3456 KiB