結果

問題 No.2064 Smallest Sequence on Grid
ユーザー koba-e964koba-e964
提出日時 2022-09-02 23:29:51
言語 Rust
(1.77.0)
結果
AC  
実行時間 634 ms / 3,000 ms
コード長 1,983 bytes
コンパイル時間 11,279 ms
コンパイル使用メモリ 385,748 KB
実行使用メモリ 21,656 KB
最終ジャッジ日時 2024-04-27 23:10:42
合計ジャッジ時間 17,329 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 0 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 0 ms
6,940 KB
testcase_13 AC 0 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 34 ms
20,196 KB
testcase_18 AC 33 ms
21,028 KB
testcase_19 AC 33 ms
20,336 KB
testcase_20 AC 411 ms
21,396 KB
testcase_21 AC 473 ms
21,656 KB
testcase_22 AC 299 ms
20,416 KB
testcase_23 AC 337 ms
20,300 KB
testcase_24 AC 337 ms
20,872 KB
testcase_25 AC 340 ms
20,176 KB
testcase_26 AC 634 ms
20,552 KB
testcase_27 AC 631 ms
20,084 KB
testcase_28 AC 34 ms
20,880 KB
testcase_29 AC 524 ms
18,012 KB
testcase_30 AC 32 ms
18,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn main() {
    input! {
        h: usize, w: usize,
        s: [String; h],
    }
    let mut tmp = vec![];
    for i in 0..h {
        tmp.extend(s[i].bytes());
    }
    let mut que = vec![(0, 0)];
    let mut ans = "".to_string();
    ans.push(tmp[0] as char);
    for _ in 0..h + w - 2 {
        let mut new = vec![];
        let mut mi = b'{';
        for &(x, y) in &que {
            if x + 1 < h {
                mi = min(mi, tmp[(x + 1) * w + y]);
            }
            if y + 1 < w {
                mi = min(mi, tmp[x * w + y + 1]);
            }
        }
        for &(x, y) in &que {
            if x + 1 < h {
                if mi == tmp[(x + 1) * w + y] {
                    new.push((x + 1, y));
                }
            }
            if y + 1 < w {
                if mi == tmp[x * w + y + 1] {
                    new.push((x, y + 1));
                }
            }
        }
        ans.push(mi as char);
        new.sort(); new.dedup();
        que = new;
    }
    println!("{}", ans);
}
0