結果
問題 | No.2731 Two Colors |
ユーザー |
|
提出日時 | 2024-04-19 21:56:43 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 177 ms / 3,000 ms |
コード長 | 1,870 bytes |
コンパイル時間 | 14,301 ms |
コンパイル使用メモリ | 381,188 KB |
実行使用メモリ | 14,080 KB |
最終ジャッジ日時 | 2024-10-11 15:00:12 |
合計ジャッジ時間 | 17,713 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#![allow(dead_code, unused_imports, unused_macros, non_snake_case)]fn main() {let [H, W] = read_words::<usize>()[..] else { return };let A = (0 .. H).map(|_| read_words::<usize>() ).collect::<Vec<_>>();let mut colors = vec![vec![None; W]; H];colors[0][0] = Some(true);colors[H - 1][W - 1] = Some(false);let mut pq = vec![BinaryHeap::new(); 2];for &(i, j, c) in &[(0, 0, 1), (H - 1, W - 1, 0)] {neighbor4(i, j, H, W, |i2, j2| {if colors[i2][j2].is_none() {pq[c].push((Reverse(A[i2][j2]), i2, j2));}});}let mut ans = 0;'main_loop: for t in 1 ..= H * W - 2 {let c = t % 2;while let Some((_, i, j)) = pq[c].pop() {if colors[i][j].is_some() {continue;}let mut fin = false;ans += 1;colors[i][j] = Some(c == 1);neighbor4(i, j, H, W, |i2, j2| {if colors[i2][j2].is_none() {pq[c].push((Reverse(A[i2][j2]), i2, j2));} else if colors[i2][j2] != colors[i][j] {fin = true;}});if fin {break 'main_loop;}break;}}println!("{}", ans);}type Int = i64;const MOD: Int = 1_000_000_007;const INF: Int = 1_000_000_000;const YESNO: [&'static str; 2] = ["Yes", "No"];use std::collections::*;use std::cmp::Reverse;fn read_line() -> String { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).ok(); buf }fn read_words<T: std::str::FromStr>() -> Vec<T> where T::Err: std::fmt::Debug { read_line().split_whitespace().map(|x| T::from_str(x).unwrap()).collect() }fn yes() { println!("{}", YESNO[0]); }fn no() { println!("{}", YESNO[1]); }fn yesno(c: bool) { println!("{}", if c { YESNO[0] } else { YESNO[1] }); }fn neighbor4<F: FnMut(usize, usize)>(i: usize, j: usize, h: usize, w: usize, mut f: F) { if i > 0 { (f)(i - 1, j); } if i < h - 1 { (f)(i + 1, j); }if j > 0 { (f)(i, j - 1); } if j < w - 1 { (f)(i, j + 1); } }