*** ameoba图****是一类树,其所有节点的值都从0到某个非负整数N,并且任何值x <N的特定节点都连接到x + 1 值x +的不同节点1。
N = 3的Ameoba图(表示为A 3)
请注意,不允许2共享3的任何一个。正好三个3必须分别属于“ 2”。
挑战
您的任务是通过贪婪地最小化节点之间的曼哈顿距离,在二维网格中归纳“增长”这些变形虫图:
- 基本情况:甲0仅仅是图形
0
。 - 感应步骤:甲N + 1是通过迭代地将新的N + 1级值的节点尽可能接近的N个值中的节点现有A产生Ñ结构。(由于最接近的位置可能已经被填充,因此只能尽可能地靠近。)
对于归纳步骤,必须遵循的一般步骤是:
for each existing node P with value N:
for each new N+1 valued node Q you need to connect to P: //this loops N+1 times
find the set of vacant spots that are minimally distant from P //by Manhattan distance
place Q in any of these vacant spots
(输出无法区分的其他过程也可以。)
A 4的增长示例:
A0 is always the same:
0
For A1 I happen to put the 1 to the right of the 0 (it has to go on one of the 4 open sides):
01
For A2 I happen to put the two 2's above and to the right of the 1:
2
012
For A3 I find that one of the six 3's I must place cannot be directly next to a 2, so I put in one of the next closest places:
3
323
0123
33 <-- this 3 is distance two away from its 2
The process continues in A4. Note that I'm iterating over each 3 and placing four 4's next to it or as close as possible, then moving to the next 3 (the order of 3's does not matter):
444
443444
4323444
4012344
44334
4444
44
Always keep in mind that nodes cannot be "shared".
程序
您编写的程序必须采用0到8(含)之间的数字,并使用上述感应式增长模式输出其有效的变形图。
超过8发生什么无关紧要。
(A 8包含46234个节点正在推动它。超出A 8的任何事物都太过分了。感谢MartinBüttner注意到这一点。)
输入应来自stdin或命令行,输出应进入stdout或文件。
示例(直接从上面获取)
Input: 0
Output:
0
Input: 1
Output:
01
Input: 2
Output:
2
012
Input: 3
Output:
3
323
0123
33
Input: 4
Output:
444
443444
4323444
4012344
44334
4444
44
*这些类型的图可能已经有一个名称。我承认我只是编造的。;)