fn main() { input!{ h: usize, w: usize, g: [Chars; h], } // 先頭座標-連結成分の個数 を管理 let mut mp = HashMap::new(); // 探索の先頭座標を入れる let mut seen = vec![vec![INF; h]; w]; let d = vec!{(!0, 0), (0, !0), (1, 0), (0, 1)}; // 上左下右 // 各マス試す for i in 0..h { for j in 0..w { if seen[i][j] != INF { continue; } // BFS { let mut cnt = 0; // 同じ色の個数 let mut dq = VecDeque::new(); dq.push_back((i, j)); seen[i][j] = i*w+j; cnt += 1; while dq.len() > 0 { let (ci, cj) = dq.pop_front().unwrap(); for k in 0..4 { let ni = ci.wrapping_add(d[k].0); let nj = cj.wrapping_add(d[k].1); if ni >= h || nj >= w || seen[ni][nj] != INF || g[ni][nj] != g[ci][cj] { continue; } seen[ni][nj] = i*w+j; dq.push_back((ni, nj)); cnt += 1; } } mp.insert(i*w+j, cnt); } } } // seenの値にある連結成分を確認して、4以上なら.にする。 let mut ans_g = g.clone(); for i in 0..h { for j in 0..w { let num = seen[i][j]; if *mp.get(&num).unwrap() >= 4 { ans_g[i][j] = '.'; } } } for i in 0..h { for j in 0..w { print!("{}", ans_g[i][j]); } println!(); } } // const MOD17: usize = 1000000007; // const MOD93: usize = 998244353; const INF: usize = 1 << 60; // let dx = vec![!0, 0, 1, 0]; // 上左下右 // let dy = vec![0, !0, 0, 1]; // 上左下右 #[allow(unused)] use proconio::{input, marker::Chars, marker::Usize1}; #[allow(unused)] use std::{ mem::swap, cmp::min, cmp::max, cmp::Reverse, collections::HashSet, collections::BTreeSet, collections::HashMap, collections::BTreeMap, collections::BinaryHeap, collections::VecDeque, iter::FromIterator, };