結果

問題 No.217 魔方陣を作ろう
ユーザー Tsuneo YoshiokaTsuneo Yoshioka
提出日時 2015-05-27 00:07:37
言語 Ruby
(3.3.0)
結果
AC  
実行時間 87 ms / 5,000 ms
コード長 1,936 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 11,268 KB
実行使用メモリ 15,296 KB
最終ジャッジ日時 2023-08-27 05:36:03
合計ジャッジ時間 3,203 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
15,124 KB
testcase_01 AC 82 ms
15,080 KB
testcase_02 AC 83 ms
15,124 KB
testcase_03 AC 85 ms
14,992 KB
testcase_04 AC 87 ms
15,284 KB
testcase_05 AC 86 ms
15,216 KB
testcase_06 AC 83 ms
15,292 KB
testcase_07 AC 83 ms
15,080 KB
testcase_08 AC 84 ms
15,076 KB
testcase_09 AC 83 ms
15,068 KB
testcase_10 AC 84 ms
14,996 KB
testcase_11 AC 85 ms
15,252 KB
testcase_12 AC 84 ms
15,076 KB
testcase_13 AC 87 ms
15,292 KB
testcase_14 AC 85 ms
15,268 KB
testcase_15 AC 85 ms
15,120 KB
testcase_16 AC 84 ms
15,296 KB
testcase_17 AC 85 ms
15,068 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i


def output(m)
    n = m.size
    (0...n).each{|r|
        (0...n).each{|c|
            if(c!=0)
                print " "
            end
            print m[r][c]
        }
        puts
    }
end


def genodd(n)
    m = Array.new(N).map{Array.new(N);}
    r=0; c=n/2
    count = 1
    while true
        if m[r][c]
            r+=2;r%=n
            c-=1;c+=n;c%=n
            if m[r][c]
                break
            end
        end
        m[r][c]=count
        count+=1
        r -=1; r+=n; r%=n;
        c +=1; c%=n;
    end
    return m
end
def gen4(n)
    m = Array.new(N).map{Array.new(N);}
    cross = [[1,0,0,1],[0,1,1,0],[0,1,1,0],[1,0,0,1]]
    count = 0
    (0...n).each{|r|
        (0...n).each{|c|
            r2 = r%4
            c2 = c%4
            if cross[r2][c2]==1
                m[r][c] = count + 1
            else
                m[r][c] = n*n - count
            end
            count +=1
        }
    }
    return m
end

L=[[4,1],[2,3]]
U=[[1,4],[2,3]]
X=[[1,4],[3,2]]
def gen4_2(n)
    n0 = n/2
    m0 = genodd(n0)
    m1 = Array.new(n0).map{Array.new(n0,'L');}
    (0...n0).each{|c0|
        m1[n0/2+1][c0] = 'U';
        (n0/2+2...n0).each{|r|
            m1[r][c0] = 'X';
        }
    }
    if(n0>=3)
        m1[n0/2+1][n0/2] = 'L'
        m1[n0/2][n0/2] = 'U'
    end
    #output(m1)
    m = Array.new(N).map{Array.new(N);}
    (0...n).each{|r|
        (0...n).each{|c|
            r2 = r%2
            c2 = c%2
            r0 = r/2
            c0 = c/2
            m[r][c] = 4*(m0[r0][c0]-1)
            if m1[r0][c0]=='L'
                m[r][c] += L[r2][c2]
            elsif m1[r0][c0]=='U'
                m[r][c] += U[r2][c2]
            elsif m1[r0][c0]=='X'
                m[r][c] += X[r2][c2]
            end
        }
    }
    return m
end


if N%2 == 1
    m = genodd(N)
    output(m)
elsif N%4==0
    m = gen4(N)
    output(m)
else
    m = gen4_2(N)
    output(m)
end
0