《艰难历程 | 像程序员一样思考》 第八集 Alex Rosenthal: The Gauntlet | Think Like A Coder, Ep 8

上映日期: 0




演员: Alex Rosenthal

Their fall from the tower sends Ethic and Hedge
Ethic 和 Hedge 从高塔坠落,
spinning into the rapids of a river of pure energy.
This torrent flows from the Bradbarrier all the way to Huxenborg.
这条来自 Bradbarrier 的激流 通向 Huxenborg。
There an entire city’s worth of factories
那里整个城市的工厂 都在制造机器人,
build the robots and house the Node of Memory,
the last of the three powerful artifacts Ethic needs to collect.
三件强力神器中,记忆节点 是 Ethic 需要收集的最后一件。
After a long day and a longer night
度过了漫长的一天 和更漫长的夜晚后,
they find themselves in a canyon of brick and steel.
他们发现自己正身处 由砖块和钢铁构成的峡谷中。
Just when they’re about to reach the end of the line,
a rope catches them.
Their savior, Lemma, has been waiting for them.
他们的救世主 Lemma 已等候多时。
When Ethic claimed the Node of Creation from the forest tower,
当 Ethic 从森林高塔取得创造节点后,
radios all across the land came back to life.
陆地上所有的无线装置 都恢复了正常运作。
Adila, the resistance leader, immediately started contacting her allies,
反抗军领袖 Adila 立即开始联系盟友,
none more important than Lemma,
首推在 Huxenborg 工作的 杰出科学家 Lemma,
a brilliant scientist working from within Huxenborg to bring down the machines.
Unfortunately, the radios also tipped off the robots.
So they’ve taken defensive measures
to protect the final artifact in its home in the very heart of the city.
来保护位于城市中心 关键区域的最后神器。
There’s only one way to get there: the gauntlet of forking paths.
要到达那里,只有一种办法: 战胜分叉路径的严酷挑战。
It’s a deadly series of luminous conveyors that wind underneath Huxenborg.
这是一系列致命的发光传送带, 在 Huxenborg 地下蜿蜒而行。
Starting from the current position,
each section runs for a distance, then splits in two.
Every branch does the same thing, again and again.
There are thousands of branches.
Only one path leads to the artifact; all the others to destruction.
通往神器的路只有一条; 其他的路径只会通向毁灭。
Fortunately, the Node of Creation has granted Hedge a strange power:
幸运的是,创造节点 赋予了 Hedge 一种神秘力量:
he can produce slightly smaller versions of himself.
Each version can do only two things: radio information back to its parent,
and produce slightly smaller versions of itself…
通过无线电向父级传回信息, 并复制两个更小的自己 ... ...
which can do the same two things, as can their children,
这些复制体与其子级复制体 也会做同样的这两件事,
for as many generations as needed.
A patrol is closing in on their position, so Ethic’s time is limited.
一支巡逻队正在接近他们, Ethic 的时间很有限。
What instructions should she give Hedge to find the one safe path?
为了找到安全路径, 她该给 Hedge 下达什么指令?
Pause the video to figure it out yourself.
[ 可暂停视频,自行解题 ]
Hint in 3
[ 答案揭晓:3 ]
Hint in 2
[ 答案揭晓:2 ]
Hint in 1
[ 答案揭晓:1 ]
Programmers have an elegant tool in their arsenal called recursion.
程序员的装备库中 有一件强大的工具叫递归,
Recursion is when you have a set of instructions that refers back to itself.
It’s like using a word in its own definition,
except where that’s frowned upon, this is incredibly effective.
除了让人觉得怪异之外, 这种方式本身非常有效。
Recursion involves repetition, but in a different way than loops.
Where a loop takes one action and repeats it again and again,
循环在执行一次操作后, 会重复执行这个过程,
recursion will start an action, and before it’s finished, use it again,
而递归在开始执行一次操作后 , 会在完成之前再次执行操作,
and before that’s finished, use it again, and so on.
完成前再次执行同样动作, 以此类推下去 ... ...
It keeps doing this until some end state is reached.
一直重复此操作, 直到满足某种条件,
It then passes the information back up, layer after layer,
until it reaches the top and ends the cycle.
Recursion is ideal for problems that involve self-similarity,
递归非常适合解决 涉及自相似性的问题,
where each part resembles the larger whole.
Like, for example, a deadly defense system designed to end any person or thing
who dares tread upon it.
Pause the video to figure it out yourself.
[ 可暂停视频,自行解题 ]
Solution in 3
[ 答案揭晓:3 ]
Solution in 2
[ 答案揭晓:2 ]
Solution in 1
[ 答案揭晓:1 ]
Ethic’s conundrum seems sprawling on the surface,
Ethic 的难题表面看起来很难,
but there’s a remarkably simple solution to it using recursion.
In order to find it, let’s first look at the simplest version of this puzzle:
为了找到这个方法, 先来看看谜题的最简版本:
what if the entire maze were just two paths?
If Hedge copies himself, the copy that goes the wrong way will be destroyed.
如果 Hedge 复制两个自己, 走入错路的复制体将被毁灭。
So the other one, which will reach the artifact,
can radio back the path it took, and then no matter which way is correct,
并用无线电发回其路径信息, 不需要考虑其他路径,
that’s the answer Hedge will receive.
那就是 Hedge 接收到的答案。
This is called the "base case" of the recursion.
这就是 “递归的基本情况”。
Now, suppose the maze branches twice from the starting point,
and at every intersection, Hedge’s copies—
在每个分支的节点 Hedge 进行自我复制 ——
let’s call them Twig 1 and Twig 2—
我们称为 分支 1 和 分支 2——
make more copies— let’s call them Leaves 1 through 4.
它们继续进行复制, 得到 叶片 1 至叶片 4,
Three Leaves will be destroyed.
The one that reaches the artifact will radio back the right answer,
到达神器的那条路径 将传回正确答案,
but only to its parent.
So if Twig 1 or 2 is waiting at an intersection
如果分支 1 或 2 在节点等待
and hears something over the radio,
that’s the right way to go to the artifact from where it is.
即从其所在位置 通往神器的正确道路。
To tell Hedge the right answer from his perspective,
要从分支的角度 告诉 Hedge 正确答案,
the Twig should say which way it went,
and then the route it just heard over the radio.
This same process will work no matter how many times the maze branches.
不管迷宫分支多少次, 同样的过程都会奏效,
Any answer a copy hears on the radio
must be the way to the control room from its location,
and if it then adds the branch it took,
it can tell its parent how to get there as well.
也就能告诉父级 如何从它的位置到控制室。
We can sum up the instructions in an action called Pathfinder
that every version of Hedge will follow:
我们总结出 Hedge 的每个复制体 需要遵循的指令如下:
1. If you’ve reached the artifact,
1. 如果到达神器地点,
radio to your parent whether you got there by going left or right.
通过无线电告知父级 选左边或右边的路径。
2. When you reach an intersection, move off the conveyor
2. 到达节点后,离开传送带,
and send new copies down the left and right paths.
Have them each run Pathfinder.
This is where recursion comes in,
and this may happen many times before the last instruction triggers, which is:
在最终指令触发前,这个过程 可能会发生很多次。最终指令是:
3. If you hear anything over the radio, you should radio to your parent
3. 如果无线接收到任何信息, 都应当传递回父级——
whether you got to your spot by going left or right,
不管你走左边还是右边的路 到达你所在位置,
then repeat everything you just heard.
Pathfinder is an example of what programmers call functions,
subroutines, or procedures.
No matter the terminology, the idea is the same—
不管用什么术语来形容, 其本质都是一样的——
it’s a set of instructions given a label so that it can be easily reused—
这只是一组带有标签的指令, 可以方便地重复使用——
perhaps even by itself.
And in our case that’ll work perfectly—
在我们的案例中, 这一方法将非常有效——
an entire network of paths mapped using just three instructions.
仅仅使用三条指令 就能映射出整个路径网络。
So here's what happens.
By the time the patrol rounds the corner, Ethic and Lemma have improvised disguises.
在巡逻队正要经过拐角时, Ethic 和 Lemma 及时伪装了自己。
They try to confuse the bots long enough to buy Hedge time.
她们试图迷惑机器人, 以便为 Hedge 争取时间,
Finally, Hedge’s radio crackles to life with a series of directions.
终于,Hedge 的无线电响起, 收到了正确路径信息。
The three dive onto the conveyor and flee for their lives,
with a squadron of enforcer bots in hot pursuit.