結果

問題 No.217 魔方陣を作ろう
ユーザー cielciel
提出日時 2015-11-01 02:07:34
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 939 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 11,180 KB
実行使用メモリ 15,324 KB
最終ジャッジ日時 2023-10-11 07:35:44
合計ジャッジ時間 3,357 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,096 KB
testcase_01 AC 82 ms
15,272 KB
testcase_02 AC 82 ms
15,016 KB
testcase_03 WA -
testcase_04 AC 83 ms
15,204 KB
testcase_05 AC 83 ms
15,012 KB
testcase_06 AC 85 ms
15,256 KB
testcase_07 AC 84 ms
15,208 KB
testcase_08 AC 83 ms
15,200 KB
testcase_09 AC 83 ms
15,100 KB
testcase_10 AC 83 ms
15,140 KB
testcase_11 WA -
testcase_12 AC 83 ms
15,120 KB
testcase_13 AC 84 ms
15,148 KB
testcase_14 AC 83 ms
15,124 KB
testcase_15 WA -
testcase_16 AC 83 ms
15,176 KB
testcase_17 AC 83 ms
15,128 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/2-2).times.map{[:l]*(n/2)} + [[:u]*(n/2)] + [[:x]*(n/2)]
	lux[n/2-2][n/4],lux[n/2-3][n/4]=lux[n/2-3][n/4],lux[n/2-2][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