解题语言不限Java
- Advent of Code Day 1 逆向验证码
- Advent of Code Day 2 损坏校验和
- Advent of Code Day 3 螺旋内存
- Advent of Code Day 4 高熵密码
- Advevnt of Code Day 5 曲折的蹦床迷宫
- Advent of Code Day 6 内存重分配
- Advent of Code Day 7 递归马戏团
- Advent of Code Day 8 注册表爱好者
- Advent of Code Day 9 流处理
- Advent of Code Day 10 结哈希
- Advent of Code Day 11 六边形迷宫
题目内容
Wandering further through the circuits of the computer, you come upon a tower of programs that have gotten themselves into a bit of trouble. A recursive algorithm has gotten out of hand, and now they're balanced precariously in a large tower.
沿着电路板向前走远,你来到了一个出来问题的程序塔。一个递归程序出错,现在他们需要平衡整个程序塔。
One program at the bottom supports the entire tower. It's holding a large disc, and on the disc are balanced several more sub-towers. At the bottom of these sub-towers, standing on the bottom disc, are other programs, each holding their own disc, and so on. At the very tops of these sub-sub-sub-...-towers, many programs stand simply keeping the disc below them balanced but with no disc of their own.
一个程序在底部支撑整个塔,它会保持一个大盘,在这个大盘上会有几个小塔。其他的程序立在这个大盘中小塔的底部,保持着一个盘子,如此如此。在这些分塔的分塔的分塔……的最顶上,很多程序立在盘上,保持底下的盘子平衡。
You offer to help, but first you need to understand the structure of these towers. You ask each program to yell out their name, their weight, and (if they're holding a disc) the names of the programs immediately above them balancing on that disc. You write this information down (your puzzle input). Unfortunately, in their panic, they don't do this in an orderly fashion; by the time you're done, you're not sure which program gave which information.
你答应去帮忙,但是最开始你要知道这些塔的结构。你让每个程序喊出他们的名字,质量,和他们支持的盘子上的程序。不幸的是,他们没有按照顺序回答,当你完成之后,你不知道是哪个程序给出的信息。
For example, if your list is the following:
比如,你列出了这些:
pbga (66)
xhth (57)
ebii (61)
havc (66)
ktlj (57)
fwft (72) -> ktlj, cntj, xhth
qoyq (66)
padx (45) -> pbga, havc, qoyq
tknk (41) -> ugml, padx, fwft
jptl (61)
ugml (68) -> gyxo, ebii, jptl
gyxo (61)
cntj (57)
...then you would be able to recreate the structure of the towers that looks like this:
然后你可以整理出这个塔的结构:
gyxo
/
ugml - ebii
/ \
| jptl
|
| pbga
/ /
tknk - - - padx - havc
\ \
| qoyq
|
| ktlj
\ /
fwft - cntj
\
xhth
In this example, tknk
is at the bottom of the tower (the bottom program), and is holding up ugml
, padx
, and fwft
. Those programs are, in turn, holding up other programs; in this example, none of those programs are holding up any other programs, and are all the tops of their own towers. (The actual tower balancing in front of you is much larger.)
在这个例子里,tknk
是在最下面的程序,同时它支持着ugml
, padx
, 和fwft
。这些程序,反过来支持着其他的程序。在最上面的程序,没有支持着任何程序。(其实你会拿到更大的塔)
Before you're ready to help them, you need to make sure your information is correct. What is the name of the bottom program?
在你帮助他们之前,你需要去确定你的信息是正确的,那么在整个塔的最底部的程序叫什么?
解题思路
其实用搜索就可以很快找到……
首先,分析过例子可以知道,输入有两种,节点(node)和终点(End)。我制作了一个递归程序来搜索所有节点的子节点,并且计算节点和。节点和是将所有终点设为1,每个节点等于子节点值的和。在上图的例子里tknk
值为九个终点的和,所以值为9
。跑完之后找最大值,就是根节点。