結果
問題 | No.228 ゆきこちゃんの 15 パズル |
ユーザー |
|
提出日時 | 2016-04-11 18:56:32 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 130 ms / 5,000 ms |
コード長 | 1,098 bytes |
コンパイル時間 | 1,859 ms |
コンパイル使用メモリ | 77,776 KB |
実行使用メモリ | 41,488 KB |
最終ジャッジ日時 | 2024-10-04 06:34:04 |
合計ジャッジ時間 | 4,846 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
import java.util.*; public class Main{ public static void main(String[] args)throws Exception{ new Main().solve(); } static int[] dx={1,-1,0,0}; static int[] dy={0,0,1,-1}; void solve(){ Scanner sc=new Scanner(System.in); int[][] table=new int[4][4]; int sx=-1; int sy=-1; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ table[i][j]=sc.nextInt(); if(table[i][j]==0){ sx=j; sy=i; } } } for(int i=0;!check(table)&&i<16;i++){ for(int j=0;j<4;j++){ int x=sx+dx[j]; int y=sy+dy[j]; if(0>x||0>y||x>=4||y>=4)continue; if(table[y][x]==4*sy+sx+1){ int dumsXsY=table[sy][sx]; table[sy][sx]=table[y][x]; table[y][x]=dumsXsY; sx=x; sy=y; break; } } } if(check(table)){ System.out.println("Yes"); }else{ System.out.println("No"); } } boolean check(int[][] table){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(table[i][j]!=4*i+j+1&&!(i==3&&j==3&&table[i][j]==0)){ return false; } } } return true; } void tr(Object...o){System.out.println(Arrays.deepToString(o));} }