結果

問題 No.283 スライドパズルと魔方陣
ユーザー cielciel
提出日時 2015-11-01 04:07:25
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,510 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 11,280 KB
実行使用メモリ 15,708 KB
最終ジャッジ日時 2023-08-27 02:01:35
合計ジャッジ時間 15,089 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
15,260 KB
testcase_01 AC 75 ms
15,248 KB
testcase_02 AC 76 ms
15,164 KB
testcase_03 AC 81 ms
15,152 KB
testcase_04 AC 76 ms
15,136 KB
testcase_05 AC 76 ms
15,300 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 78 ms
15,312 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 75 ms
15,164 KB
testcase_13 AC 75 ms
15,104 KB
testcase_14 AC 74 ms
15,340 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 80 ms
15,152 KB
testcase_19 WA -
testcase_20 AC 85 ms
15,336 KB
testcase_21 AC 140 ms
15,152 KB
testcase_22 WA -
testcase_23 AC 146 ms
15,276 KB
testcase_24 AC 145 ms
15,144 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 496 ms
15,380 KB
testcase_29 AC 546 ms
15,480 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 523 ms
15,520 KB
testcase_33 AC 535 ms
15,648 KB
testcase_34 AC 536 ms
15,648 KB
testcase_35 AC 539 ms
15,396 KB
testcase_36 AC 548 ms
15,496 KB
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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
		m[r][c]=z-m[r][c]+1 if (j==0||j==3)&&(k==1||k==2) or (j==1||j==2)&&(k==0||k==3)
	}}
	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
def sliding_parity(m)
	flat=[]
	n=m.size
	parity=0
	n.times{|y|n.times{|x|
		k=m[y][x]
		if k>0
			flat<<k
			#k-=1
			#ty,tx=k.divmod(n)
			#d=(y-ty).abs+(x-tx).abs
		else
			ty=n-1
			parity=(y-ty).abs
		end
	}}
	flat.size.times{|i|
		(i+1..flat.size-1).each{|j|
			parity+=1 if flat[i]>flat[j]
		}
	}
	parity%2
end

N=gets.to_i
a=$<.map{|e|
	e.split.map{|f|f.to_i==0 ? N*N : f.to_i}
}
if N==2
	puts :impossible
	exit
end
board=gen_magicsquare(N)
if sliding_parity(a)!=sliding_parity(board)
	if N%2==0
		board.reverse!
	else
		board[0],board[N-1]=board[N-1],board[0]
	end
end
puts :possible
puts board.map{|e|e*' '}
0