結果

問題 No.223 1マス指定の魔方陣
ユーザー fmhrfmhr
提出日時 2015-06-06 20:32:35
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 2,198 bytes
コンパイル時間 12,327 ms
コンパイル使用メモリ 233,984 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 02:20:11
合計ジャッジ時間 14,664 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 1 ms
5,376 KB
testcase_13 WA -
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
5,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 2 ms
5,376 KB
testcase_23 WA -
testcase_24 AC 1 ms
5,376 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 2 ms
5,376 KB
testcase_40 WA -
testcase_41 AC 2 ms
5,376 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
)

func main() {
	var N, X, Y, Z int
	fmt.Scan(&N, &X, &Y, &Z)
	mg := magicSquare(N)
	X--
	Y--
	//N--
	var Zi, Zj int
	for i := 0; i < N; i++ {
		for j := 0; j < N; j++ {
			if mg[i][j] == Z {
				Zi = i
				Zj = j
			}
		}
	}
	mg[Y], mg[Zi] = mg[Zi], mg[Y]
	//for i := 0; i < N; i++ {
	//	mg[i][X], mg[i][Zj] = mg[i][Zj], mg[i][X]
	//}
	//print(mg)
	a := Zj - X
	print2(mg, a)
}

func print2(z [][]int, a int) {
	var k int
	for i := range z {
		for j := range z[i] {
			if j+a >= len(z[i]) {
				k = j + a - len(z[i])
			} else if j+a < 0 {
				k = j + a + len(z[i])
			} else {
				k = j + a
			}
			fmt.Print(z[i][k], " ")
		}
		fmt.Println("")
	}
}
func print(z [][]int) {
	for i := range z {
		for j := range z[i] {
			fmt.Print(z[i][j], " ")
		}
		fmt.Println("")
	}
}

func doubleSliceInt(y, x int) [][]int {
	ss := make([][]int, y)
	for i := range ss {
		ss[i] = make([]int, x)
	}
	return ss
}

func odd(N int, z [][]int) [][]int {
	y := 0
	x := N / 2
	for i := 0; i < N*N; i++ {
		z[y][x] = i + 1
		y2 := (y + N - 1) % N
		x2 := (x + 1) % N
		if z[y2][x2] != 0 {
			y2 = (y + 1) % N
			x2 = x
		}
		y = y2
		x = x2
	}
	return z
}

func magicSquare(N int) [][]int {
	z := doubleSliceInt(N, N)
	if N%2 == 1 {
		z = odd(N, z)
	} else if N%4 == 0 {
		for i := 0; i < N; i++ {
			for j := 0; j < N; j++ {
				if i%4 == j%4 || i%4+j%4 == 3 {
					z[i][j] = N*i + j + 1
				} else {
					z[i][j] = N*N - (N*i + j)
				}
			}
		}
	} else {
		n := (N - 2) / 4
		seed := doubleSliceInt(2*n+1, 2*n+1)
		seed = odd(2*n+1, seed)
		LUX := doubleSliceInt(2*n+1+1, 2*n+1)
		for i := 0; i < 2*n+1; i++ {
			LUX[(2*n+1)/2+1][i] = 1
		}
		for i := (2*n+1)/2 + 2; i < (2*n+1)+1; i++ {
			for j := 0; j < (2*n + 1); j++ {
				LUX[i][j] = 2
			}
		}
		LUX[N/4][N/4] = 1
		LUX[1+N/4][N/4] = 0
		L := [2][2]int{{4, 1}, {2, 3}}
		U := [2][2]int{{1, 4}, {2, 3}}
		X := [2][2]int{{1, 4}, {3, 2}}
		LUXmat := [3][2][2]int{L, U, X}
		for x := 0; x < N/2; x++ {
			for y := 0; y < N/2; y++ {
				val := (seed[x][y] - 1) * 4
				idx := LUX[x][y]
				for a := 0; a < 2; a++ {
					for b := 0; b < 2; b++ {
						z[2*x+a][2*y+b] = LUXmat[idx][a][b] + val
					}
				}
			}
		}
	}
	return z
}
0