結果

問題 No.157 2つの空洞
ユーザー tonyu0tonyu0
提出日時 2020-04-04 14:04:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 2,297 bytes
コンパイル時間 1,175 ms
コンパイル使用メモリ 152,496 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-16 05:41:27
合計ジャッジ時間 2,244 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 0 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let w: usize = itr.next().unwrap().parse().unwrap();
    let h: usize = itr.next().unwrap().parse().unwrap();
    let grid: Vec<Vec<char>> = (0..h)
        .map(|_| itr.next().unwrap().chars().collect())
        .collect();

    let mut cav = Vec::new();
    let mut used = vec![false; h * w];
    let mut end = false;
    let dx: Vec<isize> = [0, -1, 0, 1, -1, 1, -1, 1].to_vec();
    let dy: Vec<isize> = [1, 0, -1, 0, -1, 1, 1, -1].to_vec();
    for i in 0..h {
        for j in 0..w {
            if grid[i][j] == '.' {
                let mut q = std::collections::VecDeque::new();
                q.push_back((i, j));
                cav.push((i, j));
                used[i * w + j] = true;
                while let Some((y, x)) = q.pop_front() {
                    for i in 0..4 {
                        let nx = x as isize + dx[i];
                        let ny = y as isize + dy[i];
                        if 0 <= nx
                            && nx < w as isize
                            && 0 <= ny
                            && ny < h as isize
                            && !used[ny as usize * w + nx as usize]
                            && grid[ny as usize][nx as usize] == '.'
                        {
                            cav.push((ny as usize, nx as usize));
                            used[ny as usize * w + nx as usize] = true;
                            q.push_back((ny as usize, nx as usize));
                        }
                    }
                }
                end = true;
                break;
            }
        }
        if end {
            break;
        }
    }

    let mut ans = 1 << 30;
    for i in 0..h {
        for j in 0..w {
            if grid[i][j] == '.' && !used[i * w + j] {
                for k in 0..cav.len() {
                    ans = std::cmp::min(
                        ans,
                        (i as isize - cav[k].0 as isize).abs()
                            + (j as isize - cav[k].1 as isize).abs()
                            - 1,
                    );
                }
            }
        }
    }
    println!("{}", ans);
}
0