在令人费解的堆栈交换上受到这个问题的启发。
给出5叠5叠5张数字从1到7的卡片,输出清空所有堆栈所需的动作。移动被定义为要将卡片取下的列的索引,并且只有在下列情况下,移动才有效:
这个挑战有松散的输入规则。只要输入在所有可能的输入中是明确的,就可以使用任何格式作为输入。同一标记必须用于卡片表示的每个实例。也就是说,如果您使用2来表示2,那么您必须在输入的任何地方使用它。但是您可以使用2来表示2,使用532jv4q0fvq!@$@VQea来表示3,如果这样做有某种好处的话。
移动列表需要清空所有堆栈而不被卡住。这种格式也是松散的,但它需要表示一个索引列表,其中0是最左边的堆栈。
您的程序可能假设提供的输入是可解的。也就是说,在无法解决输入的情况下,您可能有未定义的行为。
注意:如果输入有多个可能的解决方案,则至少输出一个有效的解决方案是可以接受的。
输入:表示5个堆栈,其中一个堆栈是5,5,3,2,6,5是堆栈的顶部。其他4层也是如此。同样,正如问题中所述,这个输入格式是松散的,可以更改以方便回答。
5 5 3 2 6
2 4 1 7 7
4 2 7 1 3
2 3 3 1 4
4 6 5 5 1输出:表示从堆栈中取出卡片的顺序的移动列表。我会解释前三步的结果。前3弹出第4堆栈的2 (2 3 3 1 4堆栈)。第二个3弹出同一堆栈中的3。然后,1弹出第二个堆栈的2。如果您在输入上遵循这些动作,您最终将得到空堆栈。
[3, 3, 1, 3, 1, 0, 4, 0, 4, 4, 2, 0, 2, 1, 1, 3, 2, 2, 0, 2, 3, 4, 0, 1, 4]发布于 2020-01-28 23:32:12
““”;WA;"ị@Ṫ;ɗ¥¥ⱮT>Ƈ2Ɗ$€ẎIḢAfƑʋƇ1,6Ʋ25¡2ịⱮ_3一个一元链接,包含整数列表(表示堆栈),并返回整数列表(表示可能的移动序列)。
迭代地工作在一个或多个列表上,每个列表包含索引1到目前为止的卡片序列,索引2中使用的堆栈序列,位置3到7的当前堆栈。因此,堆栈被3索引,并在末尾被更改为0索引。在每次迭代时,创建所有可能的移动,然后测试卡片的顺序,以检查差异是否都是-1、-6、1或6。所有有效的移动都保持不变,并处理下一个循环周期。
““”; | Prepend two empty lists
W | Wrap in a further list
Ʋ25¡ | Repeat the following 25 times:
$€ | - For each current working list:
Ɱ Ɗ | - Using each of the following as the right argument:
T | - Truthy indices (effectively here the indices of non-empty lists)
>Ƈ2 | - Keep only those greater than 2
¥ | - ... do the following as a dyad:
A | - Absolute (used here for the side effect of copying the list)
¥ | - Following as a dyad:
;" ɗ | - Concatenate to following, using zipped arguments:
ị@ | - Index into list using reversed arguments
Ṫ | - Tail, popping from list
; | - Concatenate (to the list index)
Ẏ | - Join outer lists together
ʋƇ | - Keep those lists where the following is true:
I | - Increments (vectorises)
Ḣ | - Head
A | - Absolute
fƑ 1,6 | - Invariant when filtered to only contain 1s and 6s
2ịⱮ | 2nd sublist of each (i.e. the sublist containing the list indices used)
_3 | Minus 3https://codegolf.stackexchange.com/questions/198603
复制相似问题