結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-04 14:04:48 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 2,000 ms |
| コード長 | 2,297 bytes |
| コンパイル時間 | 11,721 ms |
| コンパイル使用メモリ | 401,168 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-03 07:18:12 |
| 合計ジャッジ時間 | 12,767 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
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);
}