結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー spaciaspacia
提出日時 2016-01-20 09:07:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 1,503 bytes
コンパイル時間 2,305 ms
コンパイル使用メモリ 77,208 KB
実行使用メモリ 53,420 KB
最終ジャッジ日時 2023-10-21 13:28:21
合計ジャッジ時間 4,656 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
53,420 KB
testcase_01 AC 57 ms
53,400 KB
testcase_02 AC 58 ms
52,256 KB
testcase_03 AC 59 ms
52,400 KB
testcase_04 AC 57 ms
52,432 KB
testcase_05 AC 57 ms
52,384 KB
testcase_06 AC 58 ms
53,404 KB
testcase_07 AC 57 ms
53,392 KB
testcase_08 AC 58 ms
53,412 KB
testcase_09 AC 57 ms
53,404 KB
testcase_10 AC 59 ms
52,256 KB
testcase_11 AC 57 ms
53,412 KB
testcase_12 AC 59 ms
53,396 KB
testcase_13 AC 59 ms
53,412 KB
testcase_14 AC 59 ms
53,404 KB
testcase_15 AC 59 ms
52,260 KB
testcase_16 AC 59 ms
53,408 KB
testcase_17 AC 58 ms
53,400 KB
testcase_18 AC 59 ms
52,308 KB
testcase_19 AC 58 ms
53,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.math.*;

class Main {
	
	static int size = 4 , max = 15;
	static int[][] in = new int[size][size];
	static int[][] board = {{ 1, 2, 3, 4},
							{ 5, 6, 7, 8},
							{ 9,10,11,12},
							{13,14,15, 0}};
	static int[] dx = {0,0,-1,1};
	static int[] dy = {-1,1,0,0};
	static boolean[] isMoved = new boolean[max + 1];
	
	public static void out (Object o) {
		System.out.println(o);
	}
	
	public static boolean compArray () {
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if (in[i][j] != board[i][j]) return false;
			}
		}
		return true;
	}
	
	public static boolean solve (int x , int y) {
		if (compArray()) return true;
		for (int i = 0; i < size; i++) {
			int moveX = x + dx[i];
			int moveY = y + dy[i];
			if (moveX < 0 || moveX >= size || moveY < 0 || moveY >= size) continue;
			int n = board[moveY][moveX];
			if (isMoved[n]) continue;
			isMoved[n] = true;
			board[moveY][moveX] = 0;
			board[y][x] = n;
			if (solve(moveX , moveY)) return true;
			isMoved[n] = false;
			board[moveY][moveX] = n;
			board[y][x] = 0;
		}
		return false;
	}
	
	public static void main (String[] args) throws IOException {
		BufferedReader br = 
			new BufferedReader(new InputStreamReader(System.in));
		
		for (int i = 0; i < size; i++) {
			String[] line = br.readLine().split(" ");
			for (int j = 0; j < size; j++) {
				in[i][j] = Integer.parseInt(line[j]);
			}
		}
		
		out(solve(size - 1 , size - 1) ? "Yes" : "No");
	}
	
}
0