結果

問題 No.283 スライドパズルと魔方陣
ユーザー cielciel
提出日時 2015-11-01 04:21:50
言語 Ruby
(3.3.0)
結果
AC  
実行時間 588 ms / 2,000 ms
コード長 1,538 bytes
コンパイル時間 251 ms
コンパイル使用メモリ 11,520 KB
実行使用メモリ 15,704 KB
最終ジャッジ日時 2023-09-07 22:47:19
合計ジャッジ時間 16,302 ms
ジャッジサーバーID
(参考情報)
judge5 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
15,076 KB
testcase_01 AC 80 ms
15,240 KB
testcase_02 AC 79 ms
15,036 KB
testcase_03 AC 81 ms
15,276 KB
testcase_04 AC 80 ms
15,144 KB
testcase_05 AC 82 ms
15,024 KB
testcase_06 AC 80 ms
15,012 KB
testcase_07 AC 80 ms
15,252 KB
testcase_08 AC 84 ms
15,284 KB
testcase_09 AC 81 ms
15,316 KB
testcase_10 AC 82 ms
15,172 KB
testcase_11 AC 81 ms
15,176 KB
testcase_12 AC 82 ms
15,316 KB
testcase_13 AC 82 ms
15,140 KB
testcase_14 AC 81 ms
15,240 KB
testcase_15 AC 85 ms
15,140 KB
testcase_16 AC 88 ms
15,136 KB
testcase_17 AC 88 ms
15,148 KB
testcase_18 AC 89 ms
15,244 KB
testcase_19 AC 87 ms
15,012 KB
testcase_20 AC 90 ms
15,240 KB
testcase_21 AC 150 ms
15,280 KB
testcase_22 AC 149 ms
15,140 KB
testcase_23 AC 156 ms
15,252 KB
testcase_24 AC 158 ms
15,144 KB
testcase_25 AC 171 ms
15,140 KB
testcase_26 AC 169 ms
15,240 KB
testcase_27 AC 544 ms
15,316 KB
testcase_28 AC 542 ms
15,340 KB
testcase_29 AC 580 ms
15,464 KB
testcase_30 AC 577 ms
15,704 KB
testcase_31 AC 572 ms
15,460 KB
testcase_32 AC 577 ms
15,508 KB
testcase_33 AC 578 ms
15,348 KB
testcase_34 AC 576 ms
15,456 KB
testcase_35 AC 574 ms
15,600 KB
testcase_36 AC 588 ms
15,416 KB
testcase_37 AC 580 ms
15,372 KB
testcase_38 AC 580 ms
15,376 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
			flat<<k
			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)
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