2024-02-16 子どもの髪型を坊主にした / PAST #4 G
子どもの髪型を坊主にした。
PAST #4 第四回 アルゴリズム実技検定 過去問
- G - 村整備
https://atcoder.jp/contests/past202010-open/tasks/past202010_g
- 提出: https://atcoder.jp/contests/past202010-open/submissions/50308204
- ひとつずつ
'.'
に変えてみて連結成分数が 1 かを確認すれば良い N, M <= 10
なのでO(N^2M^2)
でも十分間に合う
use std::collections::VecDeque;
use proconio::{input, marker::Chars};
fn ok(n: usize, m: usize, s: &[Vec<char>]) -> bool {
let mut count = 0_usize;
let mut visited = vec![vec![false; m]; n];
for i in 0..n {
for j in 0..m {
if s[i][j] == '#' || visited[i][j] {
continue;
}
count += 1;
let mut deque = VecDeque::new();
visited[i][j] = true;
deque.push_back((i, j));
while let Some((r, c)) = deque.pop_front() {
let dir = vec![(-1, 0), (0, -1), (0, 1), (1, 0)];
for (dr, dc) in dir {
let (nr, nc) = (r as i64 + dr, c as i64 + dc);
if !(0..n as i64).contains(&nr) || !(0..m as i64).contains(&nc) {
continue;
}
let (nr, nc) = (nr as usize, nc as usize);
if s[nr][nc] == '#' {
continue;
}
if visited[nr][nc] {
continue;
}
visited[nr][nc] = true;
deque.push_back((nr, nc));
}
}
}
}
count == 1
}
fn main() {
input! {
n: usize,
m: usize,
mut s: [Chars; n],
};
let mut count = 0_usize;
for i in 0..n {
for j in 0..m {
if s[i][j] != '#' {
continue;
}
s[i][j] = '.';
if ok(n, m, &s) {
count += 1;
}
s[i][j] = '#';
}
}
let ans = count;
println!("{}", ans);
}
今日のコミット。
- rust-atcoder 1 commit
- genuuid 1 commit
- firestore-structured-query 2 commits
- firestore-path 2 commits
- bouzuya 1 commit