結果

問題 No.217 魔方陣を作ろう
ユーザー yaoshimaxyaoshimax
提出日時 2015-05-27 01:54:08
言語 Python2
(2.7.18)
結果
AC  
実行時間 12 ms / 5,000 ms
コード長 2,471 bytes
コンパイル時間 442 ms
コンパイル使用メモリ 6,920 KB
実行使用メモリ 6,036 KB
最終ジャッジ日時 2023-09-20 15:27:47
合計ジャッジ時間 1,665 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,956 KB
testcase_01 AC 11 ms
5,920 KB
testcase_02 AC 11 ms
5,972 KB
testcase_03 AC 11 ms
5,884 KB
testcase_04 AC 11 ms
6,036 KB
testcase_05 AC 11 ms
5,848 KB
testcase_06 AC 11 ms
5,952 KB
testcase_07 AC 12 ms
5,988 KB
testcase_08 AC 11 ms
5,880 KB
testcase_09 AC 11 ms
5,924 KB
testcase_10 AC 11 ms
5,964 KB
testcase_11 AC 11 ms
5,952 KB
testcase_12 AC 11 ms
5,928 KB
testcase_13 AC 12 ms
5,848 KB
testcase_14 AC 12 ms
6,032 KB
testcase_15 AC 12 ms
5,932 KB
testcase_16 AC 11 ms
5,884 KB
testcase_17 AC 12 ms
5,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(raw_input())
sq=[[0 for i in range(N)] for j in range(N)]
if N%2==1:
    x=0
    y=N/2
    cur=1
    while cur <= N*N:
        if sq[x][y]==0:
            sq[x][y]=cur
            cur+=1
        else:
            x+=2
            y-=1
            if y<0:
                y=N-1
            if x>=N:
                x-=N
            sq[x][y]=cur
            cur+=1
        x-=1
        y+=1
        if x<0:
            x=N-1
        if y>=N:
            y=0
elif N%4==0:
    cur=1
    for x in range(N):
        for y in range(N):
            a,b=x%4,y%4
            if a==b or a+b==3:
                sq[x][y]=cur
            cur+=1
    cur=1
    for x in range(N-1,-1,-1):
        for y in range(N-1,-1,-1):
            if sq[x][y]==0:
                sq[x][y]=cur
            cur+=1
else:
    sq2=[[0 for i in range(N/2)] for j in range(N/2)]
    n=N/2

    x=0
    y=n/2
    cur=1
    while cur <= n*n:
        if sq2[x][y]==0:
            sq2[x][y]=cur
            cur+=1
        else:
            x+=2
            y-=1
            if y<0:
                y=n-1
            if x>=n:
                x-=n
            sq2[x][y]=cur
            cur+=1
        x-=1
        y+=1
        if x<0:
            x=n-1
        if y>=n:
            y=0
    for i in range(n):
        for j in range(n):
            sq2[i][j]=4*(sq2[i][j]-1)
    for x in range(n):
        for y in range(n):
            if x == n/2 and y==n/2:
                sq[2*x][2*y]=sq2[x][y]+1
                sq[2*x+1][2*y]=sq2[x][y]+2
                sq[2*x][2*y+1]=sq2[x][y]+4
                sq[2*x+1][2*y+1]=sq2[x][y]+3
            elif x== n/2+1 and y==n/2:
                sq[2*x][2*y]=sq2[x][y]+4
                sq[2*x+1][2*y]=sq2[x][y]+2
                sq[2*x][2*y+1]=sq2[x][y]+1
                sq[2*x+1][2*y+1]=sq2[x][y]+3
            elif x<=n/2:
                sq[2*x][2*y]=sq2[x][y]+4
                sq[2*x+1][2*y]=sq2[x][y]+2
                sq[2*x][2*y+1]=sq2[x][y]+1
                sq[2*x+1][2*y+1]=sq2[x][y]+3
            elif x==n/2+1:
                sq[2*x][2*y]=sq2[x][y]+1
                sq[2*x+1][2*y]=sq2[x][y]+2
                sq[2*x][2*y+1]=sq2[x][y]+4
                sq[2*x+1][2*y+1]=sq2[x][y]+3
            else:
                sq[2*x][2*y]=sq2[x][y]+1
                sq[2*x+1][2*y]=sq2[x][y]+3
                sq[2*x][2*y+1]=sq2[x][y]+4
                sq[2*x+1][2*y+1]=sq2[x][y]+2

for i in range(N):
    for j in range(N):
        print sq[i][j],
    print
0