結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー 37zigen37zigen
提出日時 2016-04-11 18:56:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 137 ms / 5,000 ms
コード長 1,098 bytes
コンパイル時間 2,297 ms
コンパイル使用メモリ 78,164 KB
実行使用メモリ 54,392 KB
最終ジャッジ日時 2024-04-15 00:17:26
合計ジャッジ時間 5,602 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
54,144 KB
testcase_01 AC 133 ms
54,296 KB
testcase_02 AC 132 ms
54,156 KB
testcase_03 AC 135 ms
54,268 KB
testcase_04 AC 133 ms
54,096 KB
testcase_05 AC 132 ms
53,896 KB
testcase_06 AC 133 ms
53,928 KB
testcase_07 AC 134 ms
54,016 KB
testcase_08 AC 135 ms
54,076 KB
testcase_09 AC 136 ms
54,012 KB
testcase_10 AC 134 ms
54,184 KB
testcase_11 AC 132 ms
54,392 KB
testcase_12 AC 131 ms
54,208 KB
testcase_13 AC 132 ms
54,148 KB
testcase_14 AC 136 ms
54,200 KB
testcase_15 AC 132 ms
54,092 KB
testcase_16 AC 136 ms
53,796 KB
testcase_17 AC 133 ms
54,224 KB
testcase_18 AC 133 ms
54,208 KB
testcase_19 AC 134 ms
54,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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));}
}
0