我有一堆板,需要在尽可能小的空间内堆叠。不幸的是,如果我叠放超过10块高的木板,它们就会掉下来。我需要一个程序来告诉我如何堆叠电路板,以使其占用尽可能少的水平空间,而又不堆叠超过十个高的电路板,或者使电路板悬空在空的空间上。
你的任务:
编写一个程序或函数,当给定包含板长的数组时,以ASCII艺术形式输出堆叠板以节省尽可能多的水平空间的方式,而不会堆叠超过10层高的板或任何板的任何部分板闲逛在空的空间。您的ASCII艺术作品应显示板子的配置,每块板子使用不同的字符显示。最多有20个板。例如,如果输入为[2,2,4,2,2,4,4,4,4],则可能的输出为:
dhh
dgg
dff
dee
abc
abc
abc
abc
这是一个稳定的配置(尽管在现实生活中,它会在约0.1秒内掉落)。
输入:
一个包含最多20个整数的数组,显示木板的长度。
输出:
如上所述,显示板配置的ASCII文字。
测试用例:
请注意,对于测试用例,可能还有其他解决方案,并且每块板上显示的字符可能不同。
[12,2,2,2,3,4,4,8,8] -> ffgghhiii
ddddeeeeeeee
bbbbbbbbcccc
aaaaaaaaaaaa
[4,4,4,4,4,4,4,4,4,4,4,4] -> llll
aaaa
cfghk
cfghk
cfghk
cfghk
debij
debij
debij
debij
[4,4,4,4,4,4,3,3,3,2,2,2,1] -> jjml
iiil
hhhk
gggk
ffff
eeee
dddd
cccc
bbbb
aaaa
得分:
这是代码高尔夫,最低得分(以字节为单位)获胜