解題語言不限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
。跑完之后找最大值,就是根節點。