結果
| 問題 | No.228 ゆきこちゃんの 15 パズル | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-04-27 23:36:35 | 
| 言語 | Rust (1.83.0 + proconio) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 1,802 bytes | 
| コンパイル時間 | 13,604 ms | 
| コンパイル使用メモリ | 379,252 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-22 15:06:34 | 
| 合計ジャッジ時間 | 15,151 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 17 | 
ソースコード
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;
        }
    }
}
            
            
            
        