結果

問題 No.5017 Tool-assisted Shooting
ユーザー ntk-ta01ntk-ta01
提出日時 2023-07-16 19:13:51
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 5,590 bytes
コンパイル時間 1,951 ms
コンパイル使用メモリ 164,092 KB
実行使用メモリ 24,432 KB
スコア 74,830
平均クエリ数 399.74
最終ジャッジ日時 2023-07-16 19:14:05
合計ジャッジ時間 12,365 ms
ジャッジサーバーID
(参考情報)
judge17 / judge13
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
23,352 KB
testcase_01 AC 71 ms
24,048 KB
testcase_02 AC 34 ms
24,432 KB
testcase_03 AC 34 ms
24,060 KB
testcase_04 AC 44 ms
23,484 KB
testcase_05 AC 40 ms
24,300 KB
testcase_06 AC 34 ms
24,288 KB
testcase_07 AC 46 ms
23,580 KB
testcase_08 AC 36 ms
24,288 KB
testcase_09 AC 39 ms
23,376 KB
testcase_10 AC 48 ms
23,376 KB
testcase_11 AC 38 ms
23,604 KB
testcase_12 AC 33 ms
24,072 KB
testcase_13 AC 49 ms
24,360 KB
testcase_14 AC 39 ms
23,832 KB
testcase_15 AC 35 ms
23,976 KB
testcase_16 AC 33 ms
23,604 KB
testcase_17 AC 71 ms
23,772 KB
testcase_18 AC 70 ms
24,300 KB
testcase_19 AC 37 ms
24,216 KB
testcase_20 AC 44 ms
24,312 KB
testcase_21 AC 41 ms
23,964 KB
testcase_22 AC 47 ms
23,376 KB
testcase_23 AC 35 ms
23,988 KB
testcase_24 AC 45 ms
23,820 KB
testcase_25 AC 38 ms
23,568 KB
testcase_26 AC 48 ms
23,328 KB
testcase_27 AC 67 ms
24,216 KB
testcase_28 AC 39 ms
23,340 KB
testcase_29 AC 39 ms
23,532 KB
testcase_30 AC 46 ms
24,060 KB
testcase_31 AC 34 ms
23,340 KB
testcase_32 AC 38 ms
24,060 KB
testcase_33 AC 38 ms
23,352 KB
testcase_34 AC 68 ms
23,388 KB
testcase_35 AC 36 ms
23,652 KB
testcase_36 AC 48 ms
23,388 KB
testcase_37 AC 40 ms
23,784 KB
testcase_38 AC 33 ms
23,340 KB
testcase_39 AC 71 ms
23,796 KB
testcase_40 AC 50 ms
23,388 KB
testcase_41 AC 33 ms
23,400 KB
testcase_42 AC 62 ms
23,592 KB
testcase_43 AC 56 ms
24,336 KB
testcase_44 AC 34 ms
23,844 KB
testcase_45 AC 37 ms
23,328 KB
testcase_46 AC 71 ms
23,340 KB
testcase_47 AC 39 ms
23,364 KB
testcase_48 AC 72 ms
23,664 KB
testcase_49 AC 70 ms
24,288 KB
testcase_50 AC 39 ms
23,796 KB
testcase_51 AC 72 ms
23,496 KB
testcase_52 AC 87 ms
24,036 KB
testcase_53 AC 41 ms
23,376 KB
testcase_54 AC 35 ms
23,400 KB
testcase_55 AC 51 ms
24,288 KB
testcase_56 AC 35 ms
24,036 KB
testcase_57 AC 70 ms
23,364 KB
testcase_58 AC 60 ms
23,964 KB
testcase_59 AC 36 ms
24,348 KB
testcase_60 AC 53 ms
23,832 KB
testcase_61 AC 71 ms
24,300 KB
testcase_62 AC 71 ms
23,664 KB
testcase_63 AC 38 ms
23,952 KB
testcase_64 AC 35 ms
24,324 KB
testcase_65 AC 99 ms
23,496 KB
testcase_66 AC 38 ms
23,508 KB
testcase_67 AC 37 ms
24,048 KB
testcase_68 AC 43 ms
23,964 KB
testcase_69 AC 40 ms
23,784 KB
testcase_70 AC 39 ms
23,568 KB
testcase_71 AC 48 ms
23,496 KB
testcase_72 AC 36 ms
23,976 KB
testcase_73 AC 70 ms
23,376 KB
testcase_74 AC 39 ms
24,216 KB
testcase_75 AC 37 ms
23,664 KB
testcase_76 AC 41 ms
24,228 KB
testcase_77 AC 39 ms
23,412 KB
testcase_78 AC 39 ms
23,412 KB
testcase_79 AC 53 ms
23,484 KB
testcase_80 AC 39 ms
23,388 KB
testcase_81 AC 42 ms
23,388 KB
testcase_82 AC 41 ms
23,964 KB
testcase_83 AC 39 ms
23,796 KB
testcase_84 AC 36 ms
24,048 KB
testcase_85 AC 33 ms
23,376 KB
testcase_86 AC 40 ms
23,568 KB
testcase_87 AC 36 ms
23,832 KB
testcase_88 AC 34 ms
24,228 KB
testcase_89 AC 40 ms
24,276 KB
testcase_90 AC 38 ms
23,628 KB
testcase_91 AC 34 ms
23,496 KB
testcase_92 AC 70 ms
24,324 KB
testcase_93 AC 34 ms
23,964 KB
testcase_94 AC 44 ms
23,424 KB
testcase_95 AC 71 ms
24,372 KB
testcase_96 AC 60 ms
23,616 KB
testcase_97 AC 35 ms
23,352 KB
testcase_98 AC 33 ms
24,048 KB
testcase_99 AC 37 ms
23,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{
    collections::HashMap,
    io::{stdin, stdout, Write},
};

const H: usize = 60;
const W: usize = 25;

// type Output = Vec<char>;

fn main() {
    let mut sim = Sim::new();
    for _ in 1..=1000 {
        if let Some(input) = read_input() {
            sim.proceed(input.es, 'S');
            println!("S");
            stdout().flush().unwrap();
            if sim.is_defeat {
                break;
            }
        } else {
            break;
        }
    }
}

fn read_input() -> Option<Input> {
    let mut n = String::new();
    stdin().read_line(&mut n).unwrap();
    if n.trim() == "-1" {
        return None;
    }
    let n = n.trim().parse::<usize>().unwrap();
    let mut es = vec![];
    for _ in 0..n {
        let mut line = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut tokens = line.split_whitespace();
        let h = tokens.next().unwrap().parse::<i64>().unwrap();
        let p = tokens.next().unwrap().parse::<i64>().unwrap();
        let x = tokens.next().unwrap().parse::<usize>().unwrap();
        es.push((h, p, x));
    }

    Some(Input { n, es })
}

#[allow(dead_code)]
#[derive(Debug)]
struct Input {
    n: usize,
    es: Vec<(i64, i64, usize)>,
}

#[derive(Debug, Clone, Copy)]
pub struct Player {
    s: i64,
    pos_x: usize,
}

impl Player {
    pub fn new() -> Self {
        Player { s: 0, pos_x: 12 }
    }

    pub fn level(&self) -> i64 {
        1 + self.s / 100
    }
}

impl Default for Player {
    fn default() -> Self {
        Self::new()
    }
}

struct Enemy {
    hp: i64,
    dameged: i64,
    power: i64,
}

impl Enemy {
    pub fn new(hp: i64, power: i64) -> Self {
        Self {
            hp,
            dameged: 0,
            power,
        }
    }
}

pub struct Sim {
    board: Vec<Vec<Square>>,
    player: Player,
    enemies: HashMap<usize, Enemy>,
    score: i64,
    is_defeat: bool,
}

impl Sim {
    pub fn new() -> Self {
        let mut board = vec![vec![Square::EMPTY; W]; H];
        board[0][12] = Square::PLAYER;
        let player = Player::new();
        let enemies = HashMap::new();
        Self {
            board,
            player,
            enemies,
            score: 0,
            is_defeat: false,
        }
    }

    pub fn proceed(&mut self, es: Vec<(i64, i64, usize)>, command: char) {
        // 全enemyが一段下に
        for h in 0..H {
            for w in 0..W {
                if let Square::ENEMY { id: _ } = self.board[h][w] {
                    if h != 0 {
                        if Square::PLAYER == self.board[h - 1][w] {
                            self.is_defeat = true;
                        }
                        self.board[h - 1][w] = self.board[h][w];
                    }
                    self.board[h][w] = Square::EMPTY;
                }
            }
        }
        // 新たなenemyが出現
        for (h, p, x) in es {
            let id = self.enemies.len() + 1;
            self.board[H - 1][x] = Square::ENEMY { id };
            self.enemies.insert(id, Enemy::new(h, p));
        }
        // playerが移動
        match command {
            'L' => {
                let prev_x = self.player.pos_x;
                let next_x = (self.player.pos_x + W - 1) % W;
                if let Square::ENEMY { id: _ } = self.board[0][next_x] {
                    self.is_defeat = true;
                } else {
                    self.board[0][next_x] = self.board[0][prev_x];
                }
                self.board[0][prev_x] = Square::EMPTY;
                self.player.pos_x = next_x;
            }
            'R' => {
                let prev_x = self.player.pos_x;
                let next_x = (self.player.pos_x + W + 1) % W;
                self.player.pos_x = next_x;
                if let Square::ENEMY { id: _ } = self.board[0][next_x] {
                    self.is_defeat = true;
                } else {
                    self.board[0][next_x] = self.board[0][prev_x];
                }
                self.board[0][prev_x] = Square::EMPTY;
                self.player.pos_x = next_x;
            }
            'S' => {}
            _ => unreachable!(),
        }
        // playerによる攻撃
        if !self.is_defeat {
            for h in 1..H {
                if let Square::ENEMY { id } = self.board[h][self.player.pos_x] {
                    let enemy = self.enemies.get_mut(&id).unwrap();
                    enemy.dameged += self.player.level();
                    // eprintln!("id: {}", id);
                    // eprintln!("initial hp: {}", enemy.hp);
                    // eprintln!(
                    //     "hp: {}",
                    //     if enemy.hp <= enemy.dameged {
                    //         0
                    //     } else {
                    //         enemy.hp - enemy.dameged
                    //     }
                    // );
                    // eprintln!("power: {}", enemy.power);
                    // eprintln!("pos: ({}, {})", self.player.pos_x, h);
                    // eprintln!();
                    if enemy.hp <= enemy.dameged {
                        self.player.s += enemy.power;
                        self.score += enemy.hp;
                        self.board[h][self.player.pos_x] = Square::EMPTY;
                    }
                    break;
                }
            }
        }
    }
}

impl Default for Sim {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub enum Square {
    PLAYER,
    ENEMY { id: usize },
    EMPTY,
}
0