結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー atcoder8atcoder8
提出日時 2022-09-10 08:19:30
言語 Rust
(1.77.0)
結果
AC  
実行時間 55 ms / 3,000 ms
コード長 2,224 bytes
コンパイル時間 5,333 ms
コンパイル使用メモリ 151,548 KB
実行使用メモリ 7,368 KB
最終ジャッジ日時 2023-08-17 13:19:46
合計ジャッジ時間 5,773 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 29 ms
4,380 KB
testcase_08 AC 15 ms
4,412 KB
testcase_09 AC 36 ms
4,456 KB
testcase_10 AC 32 ms
4,440 KB
testcase_11 AC 36 ms
4,376 KB
testcase_12 AC 33 ms
4,376 KB
testcase_13 AC 28 ms
4,376 KB
testcase_14 AC 31 ms
7,336 KB
testcase_15 AC 31 ms
7,368 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 55 ms
6,156 KB
testcase_18 AC 8 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 42 ms
5,460 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 17 ms
4,376 KB
testcase_25 AC 25 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::BinaryHeap;

fn main() {
    let (h, w, y, x) = {
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse::<usize>().unwrap(),
            iter.next().unwrap().parse::<usize>().unwrap(),
            iter.next().unwrap().parse::<usize>().unwrap() - 1,
            iter.next().unwrap().parse::<usize>().unwrap() - 1,
        )
    };
    let mut aaa = vec![];
    for _ in 0..h {
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        aaa.push(
            line.split_whitespace()
                .map(|x| x.parse::<usize>().unwrap())
                .collect::<Vec<_>>(),
        );
    }

    let mut player_power = aaa[y][x];
    let mut visited = vec![vec![false; w]; h];
    visited[y][x] = true;
    let mut kill_cnt = 0;
    let mut heap = BinaryHeap::from(vec![(Power(0), (y, x))]);

    while let Some((enemy_power, (y, x))) = heap.pop() {
        if player_power <= enemy_power.0 {
            break;
        }

        player_power += enemy_power.0;
        kill_cnt += 1;

        if y > 0 && !visited[y - 1][x] {
            heap.push((Power(aaa[y - 1][x]), (y - 1, x)));
            visited[y - 1][x] = true;
        }
        if y < h - 1 && !visited[y + 1][x] {
            heap.push((Power(aaa[y + 1][x]), (y + 1, x)));
            visited[y + 1][x] = true;
        }
        if x > 0 && !visited[y][x - 1] {
            heap.push((Power(aaa[y][x - 1]), (y, x - 1)));
            visited[y][x - 1] = true;
        }
        if x < w - 1 && !visited[y][x + 1] {
            heap.push((Power(aaa[y][x + 1]), (y, x + 1)));
            visited[y][x + 1] = true;
        }
    }

    println!("{}", if kill_cnt == h * w { "Yes" } else { "No" });
}

#[derive(Debug, PartialEq, Eq)]
struct Power(usize);

impl std::cmp::PartialOrd for Power {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.0.partial_cmp(&other.0).unwrap().reverse())
    }
}

impl Ord for Power {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.partial_cmp(other).unwrap()
    }
}
0