結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー ccppjsrbccppjsrb
提出日時 2020-05-28 20:21:30
言語 Go
(1.21.3)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,512 bytes
コンパイル時間 11,126 ms
コンパイル使用メモリ 213,580 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-03 11:23:43
合計ジャッジ時間 12,522 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}
func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0
	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 2
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}
	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	aa := makeGrid(4, 4)
	y := -1
	x := -1
	for i := 0; i < 4; i++ {
		for j := 0; j < 4; j++ {
			aa[i][j] = getNextInt(scanner)
			if aa[i][j] == 0 {
				y = i
				x = j
			}
		}
	}
	used := make([]int, 16)
	if dfs(y, x, aa, used) {
		fmt.Fprintln(writer, "Yes")
		return
	}
	fmt.Fprintln(writer, "No")
}

func dfs(y, x int, aa [][]int, used []int) bool {
	if check(aa) {
		return true
	}

	dy := [4]int{-1, 0, 0, 1}
	dx := [4]int{0, -1, 1, 0}
	for i := 0; i < 4; i++ {
		yy := y + dy[i]
		xx := x + dx[i]
		if yy < 0 || yy >= 4 {
			continue
		}
		if xx < 0 || xx >= 4 {
			continue
		}
		if used[aa[yy][xx]] == 1 {
			continue
		}
		used[aa[yy][xx]] = 1
		aa[y][x], aa[yy][xx] = aa[yy][xx], aa[y][x]
		if dfs(yy, xx, aa, used) {
			return true
		}
		aa[y][x], aa[yy][xx] = aa[yy][xx], aa[y][x]
		used[aa[yy][xx]] = 0
	}
	return false
}
func check(aa [][]int) bool {
	for i := 0; i < 16; i++ {
		y := i / 4
		x := i % 4
		if aa[y][x] != (i+1)%16 {
			return false
		}
	}
	return true
}
func makeGrid(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}
0