The Gauntlet | Think Like A Coder, Ep 8

392,515 views ・ 2020-04-16

TED-Ed


請雙擊下方英文字幕播放視頻。

譯者: Lilian Chiu 審譯者: H_L Au
《用程式設計師的方式思考》
地點:哈林堡 地點:198 森林
第八集:挑戰
00:21
Their fall from the tower sends Ethic and Hedge
0
21734
3050
艾希克和海吉從塔上落下之後,
00:24
spinning into the rapids of a river of pure energy.
1
24784
4240
便天旋地轉地落入 純能量的急流當中。
00:30
This torrent flows from the Bradbarrier all the way to Huxenborg.
2
30944
5937
這條洪流從八達圍 一路流到哈森堡。
00:36
There an entire city’s worth of factories
3
36881
3100
在那裡,有相當於 一整個城市的工廠,
00:39
build the robots and house the Node of Memory,
4
39981
3080
用來建造機器人和存放記憶之泉,
正是艾希克必須要收集的 三項強大寶物的最後一項。
00:43
the last of the three powerful artifacts Ethic needs to collect.
5
43061
4372
00:47
After a long day and a longer night
6
47433
2510
在經過漫長的一天 和更漫長的一夜之後,
00:49
they find themselves in a canyon of brick and steel.
7
49943
3720
他們到了一個由磚塊 和鋼鐵形成的峽谷。
00:58
Just when they’re about to reach the end of the line,
8
58918
2655
他們快要到達盡頭時,
01:01
a rope catches them.
9
61573
1750
一條繩索套住了他們。
01:07
Their savior, Lemma, has been waiting for them.
10
67023
4114
他們的救星雷瑪 一直在那裡等他們。
01:11
When Ethic claimed the Node of Creation from the forest tower,
11
71137
3952
當艾希克從森林中的高塔 取得創造之泉時,
01:15
radios all across the land came back to life.
12
75089
3900
各地的無線電都恢復通訊了。
01:18
Adila, the resistance leader, immediately started contacting her allies,
13
78989
4776
反抗軍領袖艾迪拉馬上 就開始聯絡她的盟友,
01:23
none more important than Lemma,
14
83765
2610
當中最重要的就是雷瑪了,
01:26
a brilliant scientist working from within Huxenborg to bring down the machines.
15
86375
5893
她是個聰明的科學家, 在哈森堡內部努力破壞機器。
01:32
Unfortunately, the radios also tipped off the robots.
16
92268
4000
不幸的是,無線電 也將消息走漏給機器人。
01:36
So they’ve taken defensive measures
17
96268
2260
於是,它們採取了防禦措施,
01:38
to protect the final artifact in its home in the very heart of the city.
18
98528
4828
保護身處在城市核心的 最後一項寶物。
01:43
There’s only one way to get there: the gauntlet of forking paths.
19
103356
5155
要到達只有一種方法,岔路挑戰。
01:48
It’s a deadly series of luminous conveyors that wind underneath Huxenborg.
20
108511
6386
這個挑戰是在哈森堡地下 一連串的致命迂迴發光輸送帶。
01:54
Starting from the current position,
21
114897
1820
從目前的位置開始,
01:56
each section runs for a distance, then splits in two.
22
116717
4395
每段輸送帶會輸送一段距離, 接著便一分為二。
02:01
Every branch does the same thing, again and again.
23
121112
3430
每個分支都會不斷重覆這個流程。
02:04
There are thousands of branches.
24
124542
2400
總共有數千條分支。
02:06
Only one path leads to the artifact; all the others to destruction.
25
126942
5942
只有一條路通往寶物,
其他則全部都通往死亡。
02:12
Fortunately, the Node of Creation has granted Hedge a strange power:
26
132884
4619
幸運的是,創造之泉 將一種奇特的力量賦予海吉:
02:17
he can produce slightly smaller versions of himself.
27
137503
3720
它能製造出它自己的 複製品,但比較小。
02:21
Each version can do only two things: radio information back to its parent,
28
141223
5210
每一個複製品都會做兩件事:
把資訊用無線電回傳給它的母體,
02:26
and produce slightly smaller versions of itself…
29
146433
3740
以及再複製出比較小的自己,
02:30
which can do the same two things, as can their children,
30
150173
3690
而這些複製品同樣也會 做這兩件事,以此類推,
02:33
for as many generations as needed.
31
153863
2940
有必要就可以一直產生新世代。
02:36
A patrol is closing in on their position, so Ethic’s time is limited.
32
156803
5051
巡邏兵正在靠近,所以時間有限。
02:41
What instructions should she give Hedge to find the one safe path?
33
161854
5203
她應該給海吉什麼指令, 才能找到那唯一一條安全的通路?
02:47
Pause the video to figure it out yourself.
34
167057
6778
如果想要自己解題,請在此暫停。
提示即將公佈:五
02:53
Hint in 3
35
173835
990
02:54
Hint in 2
36
174825
1200
提示即將公佈:三
02:56
Hint in 1
37
176025
960
提示即將公佈:一
02:57
Programmers have an elegant tool in their arsenal called recursion.
38
177825
5162
程式設計師有項利器,叫做遞迴。
03:02
Recursion is when you have a set of instructions that refers back to itself.
39
182987
5341
當你有一組指令,又會導至 它自己本身時,就叫做遞迴。
03:08
It’s like using a word in its own definition,
40
188328
2960
就像是在一個詞的定義中 用到這個詞一樣,
03:11
except where that’s frowned upon, this is incredibly effective.
41
191288
4560
差別在於不能使用它時, 這就是非常有效的方法。
03:15
Recursion involves repetition, but in a different way than loops.
42
195848
4512
遞迴會涉及重覆,但和迴圈不同。
03:20
Where a loop takes one action and repeats it again and again,
43
200360
4000
迴圈是不斷重覆同一個動作,
03:24
recursion will start an action, and before it’s finished, use it again,
44
204360
4302
遞迴會從一個動作開始, 在它結束之前,又再次使用它,
03:28
and before that’s finished, use it again, and so on.
45
208662
4000
在那結束之前, 又再使用它,以此類推。
03:32
It keeps doing this until some end state is reached.
46
212662
4000
遞迴會這樣不斷進行下去, 直到達成某種終止狀態。
03:36
It then passes the information back up, layer after layer,
47
216662
4162
接著,就會把資訊一層一層回傳,
03:40
until it reaches the top and ends the cycle.
48
220824
3620
直到資訊傳到頂層, 循環就此結束。
03:44
Recursion is ideal for problems that involve self-similarity,
49
224444
4662
遞迴很適合用來解決 和「自相似」有關的問題,
03:49
where each part resembles the larger whole.
50
229106
2860
也就是每個部分 都和更大的整體很相似。
03:51
Like, for example, a deadly defense system designed to end any person or thing
51
231966
6004
就像是這個致命的防禦系統,
設計的目的是要消滅 任何有膽踏上它的人或物。
03:57
who dares tread upon it.
52
237970
1890
03:59
Pause the video to figure it out yourself.
53
239860
2060
如果想要自己解題,請在此暫停。
04:01
Solution in 3
54
241920
793
04:02
Solution in 2
55
242713
920
解答即將公佈
04:03
Solution in 1
56
243633
1080
04:04
Ethic’s conundrum seems sprawling on the surface,
57
244993
3041
艾希克的難題在表面上 看似雜亂無規則,
04:08
but there’s a remarkably simple solution to it using recursion.
58
248034
4366
但其實若會用遞迴, 就有一個非常簡單的解法。
04:12
In order to find it, let’s first look at the simplest version of this puzzle:
59
252400
4750
為了要解題,我們先來看看 這個謎題最簡單的版本:
04:17
what if the entire maze were just two paths?
60
257150
3270
如果整個迷宮只有兩條路呢?
04:20
If Hedge copies himself, the copy that goes the wrong way will be destroyed.
61
260420
4618
如果海吉複製它自己, 走錯路的複製品會被摧毀。
04:25
So the other one, which will reach the artifact,
62
265038
2719
所以,另一個複製 就會抵達寶物所在,
04:27
can radio back the path it took, and then no matter which way is correct,
63
267757
4700
用無線電回傳它走過的路徑資訊,
不論哪條路是對的路, 那就是海吉會收到的答案。
04:32
that’s the answer Hedge will receive.
64
272457
2401
04:34
This is called the "base case" of the recursion.
65
274858
3580
這就叫做遞迴的「基本情況」。
04:38
Now, suppose the maze branches twice from the starting point,
66
278438
3340
假設迷宮從起始點開始 就有兩倍的岔路,
04:41
and at every intersection, Hedge’s copies—
67
281778
2982
在每個路口,海吉的複製品——
04:44
let’s call them Twig 1 and Twig 2—
68
284760
2860
咱們就稱它們為 小枝一號和小枝二號——
04:47
make more copies— let’s call them Leaves 1 through 4.
69
287620
5007
會產生更多複製品—— 稱為小葉一號到小葉四號。
04:52
Three Leaves will be destroyed.
70
292627
3120
會有三個小葉複製品被摧毀。
04:55
The one that reaches the artifact will radio back the right answer,
71
295747
4116
抵達寶物所在的那個小葉 會將正確答案用無線電回傳,
04:59
but only to its parent.
72
299863
1980
但只會傳給它的母體。
05:01
So if Twig 1 or 2 is waiting at an intersection
73
301843
3880
如果小枝一號 或小枝二號在路口等候,
05:05
and hears something over the radio,
74
305723
1990
從無線電聽到消息,
05:07
that’s the right way to go to the artifact from where it is.
75
307713
3560
那就是從它的所在 通往寶物的正確路線。
05:11
To tell Hedge the right answer from his perspective,
76
311273
3290
小枝要從它的角度 把正確答案告訴海吉,
05:14
the Twig should say which way it went,
77
314563
2440
就應該要說出它去過哪裡,
05:17
and then the route it just heard over the radio.
78
317003
3600
以及它從無線電所聽到的路線。
05:20
This same process will work no matter how many times the maze branches.
79
320603
4790
不論迷宮的分岔有多少, 同樣的流程都行得通。
05:25
Any answer a copy hears on the radio
80
325393
2960
任何複製品從無線電聽到的答案
05:28
must be the way to the control room from its location,
81
328353
3300
都是從它的所在 到達控制室的路徑,
05:31
and if it then adds the branch it took,
82
331653
2350
如果再加上它自己走的岔路,
05:34
it can tell its parent how to get there as well.
83
334003
3220
它就可以告訴它的母體 如何到達那裡。
05:37
We can sum up the instructions in an action called Pathfinder
84
337223
4000
我們可以把指令總整成 一個動作,叫做「找路程式」,
05:41
that every version of Hedge will follow:
85
341223
3130
每個版本的海吉複製品 都要遵循這個程式:
05:44
1. If you’ve reached the artifact,
86
344353
2290
一、如果你到了寶物的所在,
05:46
radio to your parent whether you got there by going left or right.
87
346643
4094
用無線電告訴你的母體你怎麼走到 那裡(用左轉、右轉來表述)。
05:50
2. When you reach an intersection, move off the conveyor
88
350737
3830
二、當你到達路口時, 離開輸送帶,
05:54
and send new copies down the left and right paths.
89
354567
3980
派遣新的複製品 去走左、右兩條路。
05:58
Have them each run Pathfinder.
90
358547
2280
要求它們都要執行「找路程式」。
06:00
This is where recursion comes in,
91
360827
2040
這就是要用到遞迴的地方了,
06:02
and this may happen many times before the last instruction triggers, which is:
92
362867
5094
這個指令可能要執行許多次, 才會啟動最後一個指令,即:
06:07
3. If you hear anything over the radio, you should radio to your parent
93
367961
4987
三、如果你從無線電收到資訊, 你就要用無線電聯絡你的母體,
06:12
whether you got to your spot by going left or right,
94
372948
3629
說明你是怎麼到你的所在 (用左轉、右轉來表述),
06:16
then repeat everything you just heard.
95
376577
2850
接著再重述你從無線電 聽到的所有資訊。
06:19
Pathfinder is an example of what programmers call functions,
96
379427
3996
「找路程式」就是程式設計師
所謂的函數、子程式,或程序。
06:23
subroutines, or procedures.
97
383423
2690
06:26
No matter the terminology, the idea is the same—
98
386113
3390
不論用哪個術語稱呼, 背後的想法都相同——
06:29
it’s a set of instructions given a label so that it can be easily reused—
99
389503
4794
把一組指令包裝起來, 貼上標籤,方便重覆使用——
06:34
perhaps even by itself.
100
394297
2310
甚至可以被它自己使用。
06:36
And in our case that’ll work perfectly—
101
396607
2976
在我們的例子中, 採用它就非常完美——
06:39
an entire network of paths mapped using just three instructions.
102
399583
5230
只用三條指令 就能繪製出整個網絡。
06:46
So here's what happens.
103
406257
1630
接著發生的狀況是:
07:09
By the time the patrol rounds the corner, Ethic and Lemma have improvised disguises.
104
429760
5777
當巡邏兵到轉角時,
艾希克和雷瑪臨時湊合出偽裝。
07:15
They try to confuse the bots long enough to buy Hedge time.
105
435537
4120
她們試圖混淆機器人, 為海吉拖延時間。
07:31
Finally, Hedge’s radio crackles to life with a series of directions.
106
451473
4778
最後,海吉的無線電傳來 一連串指示,帶來了生機。
07:36
The three dive onto the conveyor and flee for their lives,
107
456251
3850
三人跳上傳輸帶,開始逃命,
07:40
with a squadron of enforcer bots in hot pursuit.
108
460101
3720
而一整隊執法機器人正緊追在後。
關於本網站

本網站將向您介紹對學習英語有用的 YouTube 視頻。 您將看到來自世界各地的一流教師教授的英語課程。 雙擊每個視頻頁面上顯示的英文字幕,從那裡播放視頻。 字幕與視頻播放同步滾動。 如果您有任何意見或要求,請使用此聯繫表與我們聯繫。

https://forms.gle/WvT1wiN1qDtmnspy7