Submission #2079278


Source Code Expand

use std::io::{BufRead, BufReader};
use std::fs::File;

// const DEBUG_MODE: u32 = 1;

/// 定数
const W: usize = 30;
const H: usize = 30;
const DIRECTIONS: [(i32, i32); 4] = [(0, -1), (-1, 0), (0, 1), (1, 0)];

/// Solver
fn solve() -> bool {
    let mut cells = read_from_file();

    // let tup = search_top(&cells);
    // println!("{}", tup.0);
    // println!("{}", tup.1);

    let mut p = search_top(&cells);
    while p.0 >= 0 {
        while true {
            let x = p.0;
            let y = p.1;
            cells[y as usize][x as usize] = cells[y as usize][x as usize] - 1;
            println!("{} {}", y+1, x+1);

            let mut next = (-1, -1);
            for d in DIRECTIONS.iter() {
                let nx = x + d.0;
                let ny = y + d.1;
                if !check_border(nx, ny) {
                    continue;
                }
                if cells[y as usize][x as usize] == cells[ny as usize][nx as usize] {
                    if cells[ny as usize][nx as usize] >= 0 {
                        next = (nx, ny);
                        break;
                    }
                }
            }

            if next.0 >= 0 {
                p = next;
                continue;
            }

            break;
        }
        p = search_top(&cells);
    }

    // for row in cells {
    //     for x in row {
    //         print!("{:>3} ", x);
    //     }
    //     println!("")
    // }
    return true;
}

fn compare_around(cells: &Vec<Vec<i32>>, x: usize, y: usize) -> bool {
    for d in DIRECTIONS.iter() {
        let dx = x as i32 + d.0;
        let dy = y as i32 + d.1;
        if !check_border(dx, dy) {
            continue;
        }
        if cells[y][x] < cells[dy as usize][dx as usize] {
            return false;
        }
    }
    return true;
}

fn search_top(cells: &Vec<Vec<i32>>) -> (i32, i32) {
    for y in 0..H {
        for x in 0..W {
            if cells[y][x] <= 0 {
                continue;
            }
            if compare_around(&cells, x, y) {
                return (x as i32, y as i32);
            }
        }
    }
    return (-1, -1);
}

fn check_border(x: i32, y: i32) -> bool {
    if x < 0 || x >= W as i32 || y < 0 || y >= H as i32 {
        return false;
    }
    return true;
}

/// 標準入力を読む
fn read_lines<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    let mut v2 = Vec::new();
    for _ in 0..n {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).ok();
        let v = s.trim()
            .split_whitespace()
            .map(|e| e.parse().ok().unwrap())
            .collect();
        v2.push(v);
    }
    v2
}

/// デバッグ用
fn read_from_file() -> Vec<Vec<i32>> {
    let mut cells = Vec::new();
    let f = File::open("src/test1.txt").unwrap();
    let fr = BufReader::new(f);

    for line in fr.lines() {
        // println!("{}", line.unwrap());
        // let s: String = line.unwrap();
        let tmp: Vec<i32> = line.unwrap()
            .trim()
            .split_whitespace()
            .map(|e| e.parse().ok().unwrap())
            .collect();
        cells.push(tmp);
    }
    cells
}

fn main() {
    solve();
}

Submission Info

Submission Time
Task A - 高橋君の山崩しゲーム
User ksakiyama134
Language Rust (1.15.1)
Score 0
Code Size 3319 Byte
Status RE
Exec Time 2 ms
Memory 4352 KB

Compile Error

warning: denote infinite loops with loop { ... }, #[warn(while_true)] on by default
  --> ./Main.rs:21:9
   |
21 |         while true {
   |         ^

warning: function is never used: `read_lines`, #[warn(dead_code)] on by default
   --> ./Main.rs:97:1
    |
97  | fn read_lines<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    | ^

Judge Result

Set Name test_01 test_02 test_03 test_04 test_05 test_06 test_07 test_08 test_09 test_10
Score / Max Score 0 / 100000 0 / 100000 0 / 100000 0 / 100000 0 / 100000 0 / 100000 0 / 100000 0 / 100000 0 / 100000 0 / 100000
Status
RE × 1
RE × 1
RE × 1
RE × 1
RE × 1
RE × 1
RE × 1
RE × 1
RE × 1
RE × 1
Set Name Test Cases
test_01 subtask_01_01.txt
test_02 subtask_01_02.txt
test_03 subtask_01_03.txt
test_04 subtask_01_04.txt
test_05 subtask_01_05.txt
test_06 subtask_01_06.txt
test_07 subtask_01_07.txt
test_08 subtask_01_08.txt
test_09 subtask_01_09.txt
test_10 subtask_01_10.txt
Case Name Status Exec Time Memory
subtask_01_01.txt RE 2 ms 4352 KB
subtask_01_02.txt RE 2 ms 4352 KB
subtask_01_03.txt RE 2 ms 4352 KB
subtask_01_04.txt RE 2 ms 4352 KB
subtask_01_05.txt RE 2 ms 4352 KB
subtask_01_06.txt RE 2 ms 4352 KB
subtask_01_07.txt RE 2 ms 4352 KB
subtask_01_08.txt RE 2 ms 4352 KB
subtask_01_09.txt RE 2 ms 4352 KB
subtask_01_10.txt RE 2 ms 4352 KB