結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー hama_du
提出日時 2015-11-24 12:34:41
言語 Go
(1.23.4)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,070 bytes
コンパイル時間 10,886 ms
コンパイル使用メモリ 221,036 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-10 20:13:41
合計ジャッジ時間 11,484 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

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