《艰难历程 | 像程序员一样思考》 第八集 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.
逃离了紧追不舍的一队机械守卫。