結果
| 問題 |
No.228 ゆきこちゃんの 15 パズル
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-06-19 22:47:31 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 117 ms / 5,000 ms |
| コード長 | 1,374 bytes |
| コンパイル時間 | 3,215 ms |
| コンパイル使用メモリ | 77,976 KB |
| 実行使用メモリ | 54,420 KB |
| 最終ジャッジ日時 | 2024-07-07 04:08:50 |
| 合計ジャッジ時間 | 5,637 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
ソースコード
import java.util.Scanner;
public class Main_yukicoder228 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
a = new int[4][4];
int sx = 0;
int sy = 0;
g = new boolean[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = sc.nextInt();
if (a[i][j] == 0) {
sx = j;
sy = i;
}
if (a[i][j] == i * 4 + j + 1) {
g[i][j] = true;
}
}
}
g[sy][sx] = true;
dfs(sx, sy);
boolean ret = true;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (!g[i][j]) {
ret = false;
}
}
}
if (ret) {
System.out.println("Yes");
} else {
System.out.println("No");
}
sc.close();
}
static int[][] a;
static boolean[][] g;
static int[] dx = {1, -1, 0, 0};
static int[] dy = {0, 0, 1, -1};
private static void dfs(int sx, int sy) {
for (int i = 0; i < 4; i++) {
int tmpx = sx + dx[i];
int tmpy = sy + dy[i];
if (tmpx >= 0 && tmpx < 4 && tmpy >= 0 && tmpy < 4) {
if (a[tmpy][tmpx] == sy * 4 + sx + 1) {
g[tmpy][tmpx] = true;
dfs(tmpx, tmpy);
break;
}
}
}
}
}