結果

問題 No.217 魔方陣を作ろう
ユーザー cielciel
提出日時 2015-11-01 02:15:57
言語 Ruby
(3.3.0)
結果
AC  
実行時間 83 ms / 5,000 ms
コード長 952 bytes
コンパイル時間 62 ms
コンパイル使用メモリ 11,472 KB
実行使用メモリ 15,300 KB
最終ジャッジ日時 2023-08-27 00:05:47
合計ジャッジ時間 3,232 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,084 KB
testcase_01 AC 82 ms
15,012 KB
testcase_02 AC 80 ms
15,116 KB
testcase_03 AC 80 ms
15,208 KB
testcase_04 AC 82 ms
15,124 KB
testcase_05 AC 80 ms
15,272 KB
testcase_06 AC 80 ms
15,264 KB
testcase_07 AC 79 ms
15,268 KB
testcase_08 AC 77 ms
15,144 KB
testcase_09 AC 77 ms
15,276 KB
testcase_10 AC 80 ms
15,132 KB
testcase_11 AC 80 ms
15,284 KB
testcase_12 AC 80 ms
15,300 KB
testcase_13 AC 80 ms
15,012 KB
testcase_14 AC 79 ms
15,128 KB
testcase_15 AC 81 ms
15,148 KB
testcase_16 AC 79 ms
15,268 KB
testcase_17 AC 79 ms
15,132 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#!/usr/bin/ruby
def gen_odd(n)
	m=n.times.map{[0]*n}
	r=n-1
	c=n/2
	i=0
	n.times{
		r=(r+1)%n
		m[r][c]=i+=1
		(n-1).times{
			r=(r+n-1)%n
			c=(c+1)%n
			m[r][c]=i+=1
		}
	}
	m
end
def gen_quad(n)
	z=n**2
	i=0
	m=n.times.map{[0]*n}
	n.times{|r|n.times{|c|
		j,k=i.divmod(n)
		j%=4
		k%=4
		m[r][c]=i+=1
		if ((j==0||j==3)&&(k==1||k==2)) || ((j==1||j==2)&&(k==0||k==3))
			m[r][c]=z-m[r][c]+1
		end
	}}
	m
end
def gen_lux(n)
	m=n.times.map{[0]*n}
	o={l:[[4,1],[2,3]],u:[[1,4],[2,3]],x:[[1,4],[3,2]]}
	lux=(n/4+1).times.map{[:l]*(n/2)} + [[:u]*(n/2)] + (n/4-1).times.map{[:x]*(n/2)}
	lux[n/4][n/4],lux[n/4+1][n/4]=lux[n/4+1][n/4],lux[n/4][n/4]
	b=gen_odd(n/2).map{|e|e.map{|f|4*(f-1)}}
	n.times{|r|n.times{|c|
		br,xr=r.divmod(2)
		bc,xc=c.divmod(2)
		m[r][c]=b[br][bc]+o[lux[br][bc]][xr][xc]
	}}
	m
end
def gen_magicsquare(n)
	if n%2==1
		gen_odd(n)
	elsif n%4==0
		gen_quad(n)
	else
		gen_lux(n)
	end
end

puts gen_magicsquare(gets.to_i).map{|e|e*' '}
0