結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー uafr_csuafr_cs
提出日時 2015-06-19 23:45:46
言語 Java19
(openjdk 21)
結果
AC  
実行時間 132 ms / 5,000 ms
コード長 2,075 bytes
コンパイル時間 2,691 ms
コンパイル使用メモリ 74,692 KB
実行使用メモリ 56,224 KB
最終ジャッジ日時 2023-09-21 10:20:29
合計ジャッジ時間 6,465 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,760 KB
testcase_01 AC 128 ms
55,576 KB
testcase_02 AC 127 ms
56,060 KB
testcase_03 AC 129 ms
55,740 KB
testcase_04 AC 127 ms
55,792 KB
testcase_05 AC 126 ms
56,224 KB
testcase_06 AC 130 ms
56,056 KB
testcase_07 AC 125 ms
55,900 KB
testcase_08 AC 126 ms
55,848 KB
testcase_09 AC 126 ms
55,520 KB
testcase_10 AC 127 ms
55,736 KB
testcase_11 AC 132 ms
55,764 KB
testcase_12 AC 126 ms
55,532 KB
testcase_13 AC 125 ms
55,816 KB
testcase_14 AC 125 ms
55,488 KB
testcase_15 AC 125 ms
55,756 KB
testcase_16 AC 126 ms
55,568 KB
testcase_17 AC 125 ms
55,748 KB
testcase_18 AC 130 ms
55,528 KB
testcase_19 AC 130 ms
55,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
	
	public static final int SIZE = 4;
	public static final int[] vs = {1, 0, -1, 0};
	
	public static boolean is_clear(int[][] board){
		for(int i = 0; i < SIZE; i++){
			for(int j = 0; j < SIZE; j++){
				final int number = ((i * SIZE) + j + 1) % 16;
				
				if(board[i][j] != number){
					return false;
				}
			}
		}
		
		return true;
	}
	
	public static boolean has_solution(int[][] board, boolean[][] moved){
		for(int i = 0; i < SIZE; i++){
			for(int j = 0; j < SIZE; j++){
				final int number = ((i * SIZE) + j + 1) % 16;
				
				if(board[i][j] != number && moved[i][j]){
					return false;
				}
			}
		}
		
		return true;
	}
	
	
	public static boolean dfs(final int x, final int y, int[][] board, boolean[][] moved){
		if(!has_solution(board, moved)){
			return false;
		}else if(is_clear(board)){
			return true;
		}
		
		for(int v = 0; v < vs.length; v++){
			final int nx = x + vs[v];
			final int ny = y + vs[(v + 1) % vs.length];
			
			if(nx < 0 || nx >= SIZE || ny < 0 || ny >= SIZE){
				continue;
			}else if(moved[ny][nx]){
				continue;
			}
			
			{
				int tmp = board[y][x];
				board[y][x] = board[ny][nx];
				board[ny][nx] = tmp;
			}
			moved[y][x] = true;
			
			if(dfs(nx, ny, board, moved)){
				return true;
			}
			
			moved[y][x] = false;
			{
				int tmp = board[y][x];
				board[y][x] = board[ny][nx];
				board[ny][nx] = tmp;
			}
		}
		
		return false;
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		boolean[][] moved = new boolean[SIZE][SIZE];
		int[][] board = new int[SIZE][SIZE];
		
		int sx = -1, sy = -1;
		for(int i = 0; i < SIZE; i++){
			for(int j = 0; j < SIZE; j++){
				board[i][j] = sc.nextInt();
				
				if(board[i][j] == 0){
					sx = j;
					sy = i;
				}
			}
		}
		
		System.out.println(dfs(sx, sy, board, moved) ? "Yes" : "No");
		
	}
}
0