CJam, 94 92 82 bytes
Esta é a versão de 92 bytes. Versão de 82 bytes a seguir.
l~1$,:L,:)m*{1bL=},\e!\m*{~W<{/(\e_}%}%{::+)-!},{{_,,\f<1fb}%2ew{:&,(},!}={{(2*'_*'[\']}/N}/
Isso divide os tijolos de todas as formas possíveis e leva apenas o que é válido. Força bruta por enquanto, mas ainda executa o último caso de teste em cerca de 10 segundos no Java Interpreter na minha máquina.
Explicação :
O código é dividido em 5 partes:
1) Dada uma variedade de comprimentos L, como todos podemos particioná-lo em Hpartes.
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
Depois disso, temos todas as formas possíveis de dividir nossa matriz de entrada em camadas de tijolo H.
2) Obtenha todas as permutações da matriz de entrada e depois obtenha ainda mais todas as partições para todas as permutações
\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
Depois disso, temos todos os layouts possíveis dos tijolos de entrada em uma Hparede de tijolos de camadas.
3) Filtre apenas os layouts cujos comprimentos de tijolo sejam iguais
{::+)-!},
{ }, 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.
Após o final desse filtro, todos os layouts restantes seriam retângulos perfeitos.
4) Retire o primeiro layout de tijolo que corresponda aos critérios de estabilidade
{{_,,\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
Após esta etapa, basta imprimir o layout
5) Imprima o layout
{{(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
Experimente online aqui
82 bytes
l~:H;{e_mrH({H-X$,+(mr)/(\e_}%_::+)-X${_,,\f<1fb}%2ew{:&,(},+,}g{{(2*'_*'[\']}/N}/
Isso é quase semelhante à versão de 92 bytes, exceto pelo fato de ter um toque de aleatoriedade. Se você leu a explicação para a versão de 92 bytes, na versão de 82 bytes, as partes 3, 4 e 5 são exatamente iguais, enquanto em vez de iterar todas as permutações das partes 1 e 2, essa versão simplesmente gera aleatoriamente um dos permutação de cada vez, testa usando as partes 3 e 4 e, em seguida, reinicia o processo se os testes das partes 3 e 4 falharem.
Isso imprime os resultados muito rapidamente para os três primeiros casos de teste. O caso de teste height = 5 ainda não deu uma saída no meu computador.
Explicação da diferença
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
A idéia para esta versão foi dada por randomra (Entendeu?)
Experimente este online