結果

問題 No.2731 Two Colors
ユーザー nautnaut
提出日時 2024-04-19 22:02:13
言語 Rust
(1.77.0)
結果
AC  
実行時間 439 ms / 3,000 ms
コード長 5,456 bytes
コンパイル時間 7,811 ms
コンパイル使用メモリ 159,232 KB
実行使用メモリ 62,080 KB
最終ジャッジ日時 2024-04-19 22:02:31
合計ジャッジ時間 9,042 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 428 ms
62,080 KB
testcase_01 AC 439 ms
62,080 KB
testcase_02 AC 427 ms
61,952 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 16 ms
5,376 KB
testcase_05 AC 11 ms
5,376 KB
testcase_06 AC 46 ms
9,344 KB
testcase_07 AC 53 ms
11,904 KB
testcase_08 AC 8 ms
5,376 KB
testcase_09 AC 199 ms
39,936 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 226 ms
31,488 KB
testcase_12 AC 219 ms
40,064 KB
testcase_13 AC 188 ms
26,752 KB
testcase_14 AC 91 ms
18,048 KB
testcase_15 AC 7 ms
5,376 KB
testcase_16 AC 89 ms
13,824 KB
testcase_17 AC 125 ms
21,248 KB
testcase_18 AC 21 ms
5,376 KB
testcase_19 AC 91 ms
14,336 KB
testcase_20 AC 132 ms
27,776 KB
testcase_21 AC 22 ms
5,376 KB
testcase_22 AC 247 ms
38,144 KB
testcase_23 AC 52 ms
10,112 KB
testcase_24 AC 24 ms
5,888 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 169 ms
32,384 KB
testcase_27 AC 214 ms
39,424 KB
testcase_28 AC 143 ms
22,912 KB
testcase_29 AC 45 ms
7,936 KB
testcase_30 AC 74 ms
14,592 KB
testcase_31 AC 49 ms
8,576 KB
testcase_32 AC 277 ms
43,904 KB
testcase_33 AC 0 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let H = input!(usize);
    let W = input!(usize);

    let A = {
        let mut a = vec![];

        for _ in 0..H {
            let b = input!(usize, W);
            a.push(b);
        }

        a
    };

    let mut pos_inv = std::collections::BTreeMap::new();

    for i in 0..H {
        for j in 0..W {
            pos_inv.insert(A[i][j], (i, j));
        }
    }

    let mut around_1 = std::collections::BTreeSet::new();
    let mut around_0 = std::collections::BTreeSet::new();

    let mut seen = vec![vec![false; W]; H];
    let mut seen1 = vec![vec![false; W]; H];
    let mut seen0 = vec![vec![false; W]; H];

    macro_rules! act1 {
        ($y: expr, $x: expr) => {
            if $x > 0 && !seen[$y][$x - 1] {
                around_1.insert(A[$y][$x - 1]);
            }

            if $y > 0 && !seen[$y - 1][$x] {
                around_1.insert(A[$y - 1][$x]);
            }

            if $x + 1 < W && !seen[$y][$x + 1] {
                around_1.insert(A[$y][$x + 1]);
            }

            if $y + 1 < H && !seen[$y + 1][$x] {
                around_1.insert(A[$y + 1][$x]);
            }
        };
    }

    macro_rules! act0 {
        ($y: expr, $x: expr) => {
            if $x > 0 && !seen[$y][$x - 1] {
                around_0.insert(A[$y][$x - 1]);
            }

            if $y > 0 && !seen[$y - 1][$x] {
                around_0.insert(A[$y - 1][$x]);
            }

            if $x + 1 < W && !seen[$y][$x + 1] {
                around_0.insert(A[$y][$x + 1]);
            }

            if $y + 1 < H && !seen[$y + 1][$x] {
                around_0.insert(A[$y + 1][$x]);
            }
        };
    }

    // (0, 0)
    seen[0][0] = true;
    seen1[0][0] = true;
    act1!(0, 0);

    // (H - 1, W - 1)
    seen[H - 1][W - 1] = true;
    seen0[H - 1][W - 1] = true;
    act0!(H - 1, W - 1);

    let mut ans = 0;

    loop {
        if ans % 2 == 0 {
            let m = around_1.range(..).next();

            match m {
                Some(&v) => {
                    ans += 1;

                    around_1.remove(&v);
                    let &(y, x) = pos_inv.get(&v).unwrap();

                    seen[y][x] = true;
                    seen1[y][x] = true;
                    act1!(y, x);

                    if around_0.contains(&v) {
                        around_0.remove(&v);
                    }

                    {
                        if x > 0 && seen0[y][x - 1] {
                            break;
                        }

                        if y > 0 && seen0[y - 1][x] {
                            break;
                        }

                        if x + 1 < W && seen0[y][x + 1] {
                            break;
                        }

                        if y + 1 < H && seen0[y + 1][x] {
                            break;
                        }
                    }
                }
                None => {
                    break;
                }
            }
        } else {
            let m = around_0.range(..).next();

            match m {
                Some(&v) => {
                    ans += 1;

                    around_0.remove(&v);
                    let &(y, x) = pos_inv.get(&v).unwrap();

                    seen[y][x] = true;
                    seen0[y][x] = true;
                    act0!(y, x);

                    if around_1.contains(&v) {
                        around_1.remove(&v);
                    }

                    {
                        if x > 0 && seen1[y][x - 1] {
                            break;
                        }

                        if y > 0 && seen1[y - 1][x] {
                            break;
                        }

                        if x + 1 < W && seen1[y][x + 1] {
                            break;
                        }

                        if y + 1 < H && seen1[y + 1][x] {
                            break;
                        }
                    }
                }
                None => {
                    break;
                }
            }
        }
    }

    writeln!(out, "{}", ans);
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0