CJam、94 92 82バイト
これは92バイトのバージョンです。82バイトバージョンが続きます。
l~1$,:L,:)m*{1bL=},\e!\m*{~W<{/(\e_}%}%{::+)-!},{{_,,\f<1fb}%2ew{:&,(},!}={{(2*'_*'[\']}/N}/
これにより、ブリックはあらゆる可能な方法に分割され、有効な方法のみが使用されます。今のところかなり強引ですが、私のマシンのJavaインタープリターで約10秒で最後のテストケースを実行します。
説明:
コードは5つの部分に分かれています。
1)lengthの配列が与えられた場合L
、どのようにそれをH
部分に分割できるか。
l~1$,:L,:)m*{1bL=},
l~ e# Read the input as string and evaluate it.
`$,:L e# Copy the array and take its length. Store that in L
,:) e# Get an array of 1 to L
m* e# Cartesian power of array 1 to L of size H (height of wall)
{1bL=}, e# Take only those parts whose sum is L
この後、入力配列をHブリックレイヤーに分割するあらゆる方法があります。
2)入力配列のすべての順列を取得し、さらにすべての順列のすべてのパーティションを取得します
\e!\m*{~W<{/(\e_}%}%
\e! e# Put the input array on top of stack and get all its permutations
\m* e# Put the all possible partition array on top and to cartesian
e# product of the two permutations. At this point, every
e# permutation of the input array is linked up with every
e# permutation of splitting L sized array into H parts
{ }% e# Run each permutation pair through this
~W< e# Unwrap and remove the last part from the partition permutation
{ }% e# For each part of parts permutation array
/ e# Split the input array permutation into size of that part
(\ e# Take out the first part and put the rest of the parts on top
e_ e# Flatten the rest of the parts so that in next loop, they can be
e# split into next part length
この後、入力ブリックの可能なすべてのレイアウトがH
レイヤーブリックウォールになります。
3)ブリックの長さが同じレイアウトのみを除外する
{::+)-!},
{ }, e# Filter all brick layouts on this condition
::+ e# Add up brick sizes in each layer
)-! e# This checks if the array contains all same lengths.
このフィルターの終了後、残りのレイアウトはすべて完全な長方形になります。
4)安定性基準に一致する最初のブリックレイアウトを取り出す
{{_,,\f<1fb}%2ew{:&,(},!}=
{ }= e# Choose the first array element that leaves truthy on stack
{ }% e# For each brick layer
_,, e# Create an array of 0 to layer length - 1
\f< e# Get all sublists starting at 0 and ending at 0
e# through length - 1
1fb e# Get sum of each sub list. This gives us the cumulative
e# length of each brick crack except for the last one
2ew e# Pair up crack lengths for every adjacent layer
{ }, e# Filter layer pairs
:& e# See if any cumulative crack length is same in any two
e# adjacent layers. This means that the layout is unstable
,( e# make sure that length of union'd crack lengths is greater
e# than 1. 1 because 0 will always be there.
! e# If any layer is filtered through this filter,
e# it means that the layer is unstable. Thus negation
このステップの後、レイアウトを印刷するだけです
5)レイアウトを印刷する
{{(2*'_*'[\']}/N}/
{ }/ e# For each brick layer
{ }/ e# For each brick
(2*'_* e# Get the (brick size - 1) * 2 underscores
'[\'] e# Surround with []
N e# Newline after each layer
こちらからオンラインでお試しください
82バイト
l~:H;{e_mrH({H-X$,+(mr)/(\e_}%_::+)-X${_,,\f<1fb}%2ew{:&,(},+,}g{{(2*'_*'[\']}/N}/
これは、92バイトバージョンとほとんど同じですが、ランダム性が少しあります。92バイトバージョンの説明を読んだ場合、82バイトバージョンでは、パート3、4、および5はまったく同じですが、パート1および2からのすべての順列を繰り返すのではなく、このバージョンは単に一度に順列し、パート3と4を使用してテストし、パート3と4のテストが失敗した場合にプロセスを再起動します。
これにより、最初の3つのテストケースの結果が非常にすばやく出力されます。height = 5のテストケースは、まだコンピューターに出力されていません。
違いの説明
l~:H;{e_mrH({H-X$,+(mr)/(\e_}%_::+)-X${_,,\f<1fb}%2ew{:&,(},+,}g
l~:H; e# Eval the input and store the height in H
{ ... }g e# A do-while loop to iterate until a solution is found
e_mr e# Flatten the array and shuffle it.
H({ }% e# This is the random partition generation loop
e# Run the loop height - 1 times to get height parts
H-X$,+( e# While generating a random size of this partition, we
e# have to make sure that the remaining parts get at least
e# 1 brick. Thus, this calculation
mr) e# Get a random size. Make sure its at least 1
/(\e_ e# Similar to 92's part 2. Split, pop, swap and flatten
_::+)- e# 92's part 3. Copy and see if all elements are same
X${_,,\f<1fb}%2ew{:&,(}, e# 92's part 4. Copy and see if layers are stable
+, e# Both part 3 and 4 return empty array if
e# the layout is desirable. join the two arrays and
e# take length. If length is 0, stop the do-while
このバージョンのアイデアは、randomra(Get it?)によって与えられました。
これをオンラインで試す