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给出的(明白吗?)
在线尝试这个