結果

問題 No.217 魔方陣を作ろう
ユーザー fmhrfmhr
提出日時 2015-05-27 06:53:30
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,590 bytes
コンパイル時間 11,219 ms
コンパイル使用メモリ 221,968 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 02:08:02
合計ジャッジ時間 12,096 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

import (
	"fmt"
)

func odd(N int, z [][]int) [][]int{
	row := 0
	col := N / 2
	for i := 0; i < N*N; i++ {
		z[row][col] = i + 1
		nrow := (row + N - 1) % N
		ncol := (col + 1) % N
		if z[nrow][ncol] != 0 {
			nrow = (row + 1) % N
			ncol = col
		}
		row = nrow
		col = ncol
	}
	return z
}

func print(z [][]int){
	for i := range z {
		for j:= range z[i] {
			fmt.Print(z[i][j], " ")
		}
	fmt.Println("")
	}
}

func main() {
	var N int
	fmt.Scan(&N)
	z := make([][]int, N)
	for i := 0; i < N; i++ {
		z[i] = make([]int, 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 := make([][]int, 2*n+1) //+1
		for i:= range seed{
			seed[i] = make([]int, 2*n+1)
		}
		seed = odd(2*n+1, seed)

		LUX := make([][]int, 2*n+1+1) //+1
		for i:= range LUX{
			LUX[i] = make([]int, 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
			}
		}
		//print(LUX)
		LUX[N/4][N/4] = 1
		LUX[1 + N/4][N/4] = 0
		//print(LUX)
		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
					}
				}

			}
		}

	}
	print(z)
}
0