結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
15,124 KB
testcase_01 AC 75 ms
15,172 KB
testcase_02 AC 75 ms
15,048 KB
testcase_03 AC 81 ms
15,272 KB
testcase_04 AC 75 ms
15,316 KB
testcase_05 WA -
testcase_06 AC 77 ms
15,312 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 76 ms
15,320 KB
testcase_11 WA -
testcase_12 AC 74 ms
15,352 KB
testcase_13 WA -
testcase_14 AC 77 ms
15,156 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 83 ms
15,204 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 84 ms
15,288 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 150 ms
15,320 KB
testcase_24 AC 148 ms
15,156 KB
testcase_25 AC 164 ms
15,148 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 508 ms
15,468 KB
testcase_29 AC 539 ms
15,472 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 534 ms
15,416 KB
testcase_33 AC 553 ms
15,584 KB
testcase_34 AC 563 ms
15,632 KB
testcase_35 AC 537 ms
15,620 KB
testcase_36 AC 542 ms
15,520 KB
testcase_37 AC 550 ms
15,656 KB
testcase_38 AC 546 ms
15,384 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
		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!=n*n
			flat<<k
			#k-=1
			#ty,tx=k.divmod(n)
			#d=(y-ty).abs+(x-tx).abs
		else
			tx=ty=n-1
			parity=(y-ty).abs+(x-tx).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)
#p sliding_parity(a)
#p sliding_parity(board)
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