結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー hama_duhama_du
提出日時 2015-11-24 12:34:41
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,070 bytes
コンパイル時間 13,111 ms
コンパイル使用メモリ 244,316 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 03:36:13
合計ジャッジ時間 11,534 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main
import (
	"fmt"
)

var DX = []int{0, -1, 0, 1}
var DY = []int{1, 0, -1, 0}

func main() {
	correct := make([][]int, 4)
	table := make([][]int, 4)
	for i := 0 ; i < 4 ; i++ {
		correct[i] = make([]int, 4)
		table[i] = make([]int, 4)
	}
	for i := 0 ; i < 4 ; i++ {
		for j := 0 ; j < 4 ; j++ {
			fmt.Scan(&table[i][j])
			correct[i][j] = (i*4+(j+1))%16
		}
	}

	if check(table, correct) {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

func check(table, correct [][]int) bool {
	same := true
	for i := 0 ; i < 4 ; i++ {
		for j := 0; j < 4; j++ {
			if table[i][j] != correct[i][j] {
				same = false
			}
		}
	}
	if same {
		return true
	}

	for i := 0 ; i < 4 ; i++ {
		for j := 0; j < 4; j++ {
			if table[i][j] == 0 {
				want := correct[i][j]
				for d := 0 ; d < 4 ; d++ {
					ti := i + DY[d]
					tj := j + DX[d]
					if ti < 0 || ti >= 4 || tj < 0 || tj >= 4 {
						continue
					}
					if table[ti][tj] == want {
						table[i][j] = want
						table[ti][tj] = 0
						return check(table, correct)
					}
				}
			}
		}
	}
	return false
}
0