Submission #74277348


Source Code Expand

use proconio::input;
use std::collections::VecDeque;

fn main() {
    input! {
        h: usize,
        w: usize,
        s: [String; h],
    }

    let mut visited = vec![vec![false; w]; h];
    let mut ans = 0;

    // 移動方向 (右,左,下,上)
    let dx = [0isize, 0, 1, -1];
    let dy = [1isize, -1, 0, 0];

    // 全マス探索
    for i in 0..h {
        for j in 0..w {
            if s[i].as_bytes()[j] == b'.' && !visited[i][j] {
                let mut touches_boundary = false;
                let mut queue = VecDeque::new();
                
                // BFS 開始
                queue.push_back((i, j));
                visited[i][j] = true;

                while let Some((r, c)) = queue.pop_front() {
                    if r == 0 || r == h - 1 || c == 0 || c == w - 1 {
                        touches_boundary = true;
                    }

                    // 隣接マス探索
                    for k in 0..4 {
                        let nr = r as isize + dx[k];
                        let nc = c as isize + dy[k];

                        // 範囲内か確認
                        if nr >= 0 && nr < h as isize && nc >= 0 && nc < w as isize {
                            let nr = nr as usize;
                            let nc = nc as usize;
                            // 白マスかつ未訪問ならキューに追加
                            if s[nr].as_bytes()[nc] == b'.' && !visited[nr][nc] {
                                visited[nr][nc] = true;
                                queue.push_back((nr, nc));
                            }
                        }
                    }
                }

                if !touches_boundary {
                    ans += 1;
                }
            }
        }
    }

    println!("{}", ans);
}

Submission Info

Submission Time
Task C - Puddles
User memoka
Language Rust (rustc 1.89.0)
Score 300
Code Size 1878 Byte
Status AC
Exec Time 26 ms
Memory 4148 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 0 ms 1840 KiB
random_01.txt AC 10 ms 4008 KiB
random_02.txt AC 3 ms 2672 KiB
random_03.txt AC 10 ms 3992 KiB
random_04.txt AC 5 ms 3128 KiB
random_05.txt AC 18 ms 3992 KiB
random_06.txt AC 1 ms 2084 KiB
random_07.txt AC 18 ms 4148 KiB
random_08.txt AC 1 ms 1976 KiB
random_09.txt AC 26 ms 4072 KiB
random_10.txt AC 20 ms 3400 KiB
random_11.txt AC 26 ms 4132 KiB
random_12.txt AC 1 ms 2052 KiB
random_13.txt AC 21 ms 3944 KiB
random_14.txt AC 2 ms 2184 KiB
random_15.txt AC 21 ms 4100 KiB
random_16.txt AC 7 ms 2600 KiB
random_17.txt AC 3 ms 4000 KiB
random_18.txt AC 12 ms 3936 KiB
random_19.txt AC 12 ms 4028 KiB
random_20.txt AC 7 ms 3900 KiB
random_21.txt AC 7 ms 4000 KiB
sample_01.txt AC 0 ms 1816 KiB
sample_02.txt AC 0 ms 1956 KiB