CJam, 94 92 82 bajtów
To jest wersja 92 bajtów. Następuje wersja 82-bajtowa.
l~1$,:L,:)m*{1bL=},\e!\m*{~W<{/(\e_}%}%{::+)-!},{{_,,\f<1fb}%2ew{:&,(},!}={{(2*'_*'[\']}/N}/
To dzieli cegły na wszystkie możliwe sposoby i bierze tylko ten, który jest ważny. Jak na razie dość brutalna siła, ale nadal wykonuje ostatni test w około 10 sekundach na Java Interpreter na moim komputerze.
Objaśnienie :
Kod jest podzielony na 5 części:
1) Biorąc pod uwagę tablicę długości L
, w jaki sposób możemy podzielić ją na H
części.
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
Następnie mamy wszystkie możliwe sposoby podziału naszej tablicy wejściowej na warstwy cegieł H.
2) Uzyskaj wszystkie permutacje tablicy wejściowej, a następnie uzyskaj wszystkie partycje dla wszystkich permutacji
\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
Następnie mamy wszystkie możliwe układy cegieł wejściowych w H
ceglany mur warstwowy.
3) Filtruj tylko te układy, których długości cegieł są takie same
{::+)-!},
{ }, 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.
Po zakończeniu tego filtra wszystkie pozostałe układy byłyby idealnymi prostokątami.
4) Wyjmij pierwszy układ klocków, który spełnia kryteria stabilności
{{_,,\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
Po tym kroku musimy po prostu wydrukować układ
5) Wydrukuj układ
{{(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
Wypróbuj online tutaj
82 bajty
l~:H;{e_mrH({H-X$,+(mr)/(\e_}%_::+)-X${_,,\f<1fb}%2ew{:&,(},+,}g{{(2*'_*'[\']}/N}/
Jest to prawie podobne do wersji 92-bajtowej, z tym wyjątkiem, że ma odrobinę losowości. Jeśli przeczytałeś wyjaśnienie dla wersji 92-bajtowej, to w wersji 82-bajtowej części 3, 4 i 5 są dokładnie takie same, a zamiast iteracji po wszystkich permutacjach z części 1 i 2, ta wersja po prostu losowo generuje jeden z permutacja na raz, testuje ją za pomocą części 3 i 4, a następnie ponownie uruchamia proces, jeśli testy części 3 i 4 nie powiodą się.
To bardzo szybko drukuje wyniki dla pierwszych 3 przypadków testowych. Przypadek testowy wysokość = 5 nie ma jeszcze danych wyjściowych na moim komputerze.
Wyjaśnienie różnicy
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
Pomysł na tę wersję został podany przez randomra (rozumiesz?)
Wypróbuj ten online