結果

問題 No.2731 Two Colors
ユーザー かもめのうでかもめのうで
提出日時 2024-04-20 16:59:51
言語 Rust
(1.77.0)
結果
AC  
実行時間 199 ms / 3,000 ms
コード長 2,517 bytes
コンパイル時間 5,686 ms
コンパイル使用メモリ 158,592 KB
実行使用メモリ 22,172 KB
最終ジャッジ日時 2024-04-20 17:00:01
合計ジャッジ時間 5,146 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 199 ms
20,608 KB
testcase_01 AC 197 ms
20,492 KB
testcase_02 AC 198 ms
20,608 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 7 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 19 ms
5,700 KB
testcase_07 AC 19 ms
6,244 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 63 ms
17,792 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 95 ms
17,880 KB
testcase_12 AC 63 ms
17,792 KB
testcase_13 AC 77 ms
15,344 KB
testcase_14 AC 30 ms
8,832 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 36 ms
8,576 KB
testcase_17 AC 45 ms
11,264 KB
testcase_18 AC 10 ms
5,376 KB
testcase_19 AC 38 ms
8,744 KB
testcase_20 AC 39 ms
12,240 KB
testcase_21 AC 10 ms
5,376 KB
testcase_22 AC 78 ms
18,464 KB
testcase_23 AC 20 ms
6,216 KB
testcase_24 AC 11 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 56 ms
15,056 KB
testcase_27 AC 71 ms
18,432 KB
testcase_28 AC 54 ms
12,264 KB
testcase_29 AC 18 ms
5,376 KB
testcase_30 AC 28 ms
7,936 KB
testcase_31 AC 22 ms
5,828 KB
testcase_32 AC 99 ms
22,172 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 0 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused)]
// use itertools::Itertools;
use proconio::*;
use std::{cmp::Reverse, collections::*};
const DIJ4: [(usize, usize); 4] = [(0, !0), (!0, 0), (0, 1), (1, 0)];
fn main() {
    input! {
        h: usize, w: usize,
        a: [[usize; w]; h]
    }
    let mut one_heap = BinaryHeap::new();
    let mut zero_heap = BinaryHeap::new();
    let nv = nvs(0, 0, h, w);
    for (ni, nj) in nv {
        one_heap.push(Reverse((a[ni][nj], ni, nj)));
    }
    let nv = nvs(h-1, w-1, h, w);
    for (ni, nj) in nv {
        zero_heap.push(Reverse((a[ni][nj], ni, nj)));
    }
    
    let mut seen = vec![vec![-1; w]; h];
    seen[0][0] = 1;
    seen[h-1][w-1] = 0;

    for t in 0..h*w {
        if t%2 == 0 {
            let (i, j) = {
                let (mut i, mut j) = (h, w);
                loop {
                    let Reverse((_, ti, tj)) = one_heap.pop().unwrap();
                    if seen[ti][tj] == -1 {
                        (i, j) = (ti, tj);
                        break;
                    }
                }
                (i, j)
            };
            seen[i][j] = 1;
            let nv = nvs(i, j, h, w);
            for (ni, nj) in nv {
                if seen[ni][nj] == 0 {
                    println!("{}", t+1);
                    return;
                }
                if seen[ni][nj] == -1 {
                    one_heap.push(Reverse((a[ni][nj], ni, nj)));
                }
            }
        }
        else {
            let (i, j) = {
                let (mut i, mut j) = (h, w);
                loop {
                    let Reverse((_, ti, tj)) = zero_heap.pop().unwrap();
                    if seen[ti][tj] == -1 {
                        (i, j) = (ti, tj);
                        break;
                    }
                }
                (i, j)
            };
            seen[i][j] = 0;
            let nv = nvs(i, j, h, w);
            for (ni, nj) in nv {
                if seen[ni][nj] == 1 {
                    println!("{}", t+1);
                    return;
                }
                if seen[ni][nj] == -1 {
                    zero_heap.push(Reverse((a[ni][nj], ni, nj)));
                }
            }
        }
    }
}

fn nvs(i: usize, j: usize, h: usize, w: usize) -> Vec<(usize, usize)> {
    let mut ret = vec![];
    for (di, dj) in DIJ4 {
        let ni = i.wrapping_add(di);
        let nj = j.wrapping_add(dj);
        if !(ni < h && nj < w) {
            continue;
        }
        ret.push((ni, nj))
    }
    ret
}
0