結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー tonyu0tonyu0
提出日時 2020-04-27 23:36:35
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,802 bytes
コンパイル時間 868 ms
コンパイル使用メモリ 148,084 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-14 16:26:43
合計ジャッジ時間 1,924 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 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,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();

    let mut table = vec![vec![0i32; 4]; 4];
    let mut cx = 0i32;
    let mut cy = 0i32;
    for i in 0..4 {
        for j in 0..4 {
            table[i][j] = itr.next().unwrap().parse().unwrap();
            if table[i][j] == 0 {
                cx = j as i32;
                cy = i as i32;
            }
        }
    }
    let dx: Vec<i32> = [0, -1, 0, 1].to_vec();
    let dy: Vec<i32> = [1, 0, -1, 0].to_vec();

    loop {
        let target = cy * 4 + cx + 1;
        let mut moved = false;
        let mut tigau = 0;
        for i in 0..4 {
            let nx = cx + dx[i];
            let ny = cy + dy[i];
            if 0 <= nx && nx < 4 && 0 <= ny && ny < 4 {
                if table[ny as usize][nx as usize] != ny * 4 + nx + 1 {
                    tigau += 1;
                }
                if table[ny as usize][nx as usize] == target {
                    moved = true;
                    table[cy as usize][cx as usize] = table[ny as usize][nx as usize];
                    table[ny as usize][nx as usize] = 0;
                    cx = nx;
                    cy = ny;
                    break;
                }
            }
        }
        if !moved {
            if tigau > 0 {
                println!("No",);
            } else {
                for i in 0..4 {
                    for j in 0..4 {
                        if table[i][j] != (i * 4 + j + 1) as i32 % 16 {
                            println!("No",);
                            return;
                        }
                    }
                }
                println!("Yes",);
            }
            return;
        }
    }
}
0