The Tower of Epiphany | Think Like A Coder, Ep 7

430,906 views ・ 2020-02-27

TED-Ed


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

譯者: Clement Fu 審譯者: Thomas Tam
[TED-Ed 原創系列]
《時代女神 x 程式邏輯革命》
[地點:一九八樹林]
[第七集:覺醒]
00:31
Ethic and Hedge are on the ground floor of a massive tower.
0
31587
5701
踏進巨塔的底層
彥霖和凌杰要先越過能量布陣
00:37
Barriers of energy separate them from their quest’s second goal:
1
37288
4657
才能觸及第二枚瑰寶:創造之泉
00:41
the Node of Creation.
2
41945
2000
00:52
To reach it, Ethic must use three energy streams to climb the tower.
3
52667
4742
彥霖要藉三道能量流登上塔頂
00:57
As soon as she steps forward a timer will begin counting down from 60 seconds.
4
57409
5950
足下一步,啟動計時器倒數 60 秒
01:07
At the back of the room there’s a basin made of invisible towers
5
67359
4300
在房間的盡頭,有一個能量池
一座座隱形能量塔一字排開
01:11
that can hold energy between them.
6
71659
3076
中間的空格可以蓄積能量
01:14
After one minute, a torrent of energy will pour down from above,
7
74735
4130
一分鐘後,能量會如急流般瀉下
01:18
filling one unit at a time,
8
78865
2150
每次填滿一個空格
01:21
with a force field preventing it from spilling out the front or back.
9
81015
4480
力場則在前後防止流溢
01:25
During the 60 calm seconds,
10
85495
2130
在風平浪靜的 60 秒間
01:27
Ethic and Hedge must decide exactly how many units of energy will fall.
11
87625
5098
彥霖、凌杰要決定降下多少格能量
01:32
For each of the three challenges,
12
92723
1700
在三關挑戰中,每關都要調好份量
01:34
they must choose the amount that will fill the basin exactly.
13
94423
3665
能量池滿而不溢才算圓滿
01:38
If they do so, the energy will propel them further upwards.
14
98088
3850
能量階梯就會將他們推上下一關
01:41
But if they get the amount at all wrong, the energy lift will fail,
15
101938
4620
計較份量若有閃失,能量階梯便破解
01:46
dropping them.
16
106558
1490
一腳踩空,就是無情的迫降
01:48
Diagrams on the walls illustrate some examples.
17
108048
3300
牆上的示意圖,枚舉幾個例子
01:51
This configuration will capture exactly 2 units of energy.
18
111348
4270
這範例蓄積恰恰兩格能量
01:55
This configuration will capture 4— 3 here, and 1 here.
19
115618
5117
範例二則蓄積四格:
第二欄 3 格、第三欄 1 格
02:00
And this one will also capture 4,
20
120735
2540
範例三也是蓄積四格:
02:03
because any energy on the right would spill out.
21
123275
3413
左右兩側框不住,能量會外溢
02:06
The energy will rain down in such a way
22
126688
2220
能量雨降下後會盡量往空子鑽進去
02:08
that it’ll only overflow if there’s no space that could hold it.
23
128908
4630
當所有空格填滿才會滿溢
02:13
Hedge can make one tower of blocks visible at a time and count how tall it is,
24
133538
5327
凌杰可逐次顯現一座能量塔再數高度
02:18
but he can’t look at the whole structure all at once.
25
138865
3860
但不能一窺整個能量池的全豹
02:22
How does Ethic program Hedge to figure out
26
142725
2805
彥霖要為凌杰設計一個怎樣的程式
02:25
exactly how much energy each basin can hold?
27
145530
3810
來找出能量池框得下多少格能量?
02:29
Pause now to figure it out for yourself.
28
149340
9465
若要自己動腦筋,就按一下暫停
02:38
Here’s one way of thinking about what’s happening:
29
158805
2830
這道題可以這樣入手:
02:41
each unoccupied cell will hold energy
30
161635
2915
但凡一個空格,要框得住能量
02:44
if and only if there is a wall eventually to its left,
31
164550
4240
最左邊,必須總得有一堵牆框住
02:48
and a wall eventually to its right.
32
168790
2727
最右邊,也得有一堵牆框住
02:51
But it would take a long time for Hedge to check this for each individual cell.
33
171517
4805
但凌杰每一格都左顧右盼會很費時
02:56
So what if he were to consider a whole column of blocks at a time?
34
176322
4863
假如凌杰逐欄來看每一座能量塔呢
03:01
How many units of energy can this hold, for instance?
35
181185
3840
比如說,這裡框得住多少格能量?
03:05
Pause now to figure it out for yourself.
36
185025
5364
[若要自己動腦筋,就按一下暫停]
03:10
Let’s analyze the problem by looking at our example.
37
190389
3370
回來剛才的例子,去分析這道題
03:13
There are 5 columns of blocks here.
38
193759
2155
能量格疊成塔,計有 5 欄
03:15
The leftmost one can’t hold any energy, because there’s nothing higher than it.
39
195914
4570
最左一欄已到頂,框不下任何能量
03:20
The 2nd stack can have 3 units above it,
40
200484
2634
第二欄,上面可再加 3 格
03:23
as they would be trapped between these two 4 block stacks.
41
203118
4126
左右這兩座四格塔,能量框得住
03:27
We get 3 units by taking the height where the energy would level off— 4,
42
207244
4942
算法是將能量塔 最高平整的高度——4
03:32
and subtracting the height of the stack— so that’s 4 minus 1.
43
212186
4160
減去能量塔本身的高度——1
即 4 減 1 得 3
03:36
The 3rd stack is similar— 4 to the left, 4 to the right, and it’s 3 high,
44
216346
5462
第三欄同理:左 4、右 4、高 3 格
03:41
so it’ll hold 4 minus 3 equals 1 unit.
45
221808
4729
故可蓄積 4 減 3 格,即 1 格
03:46
The 4th stack and 5th stacks have nothing higher than them to the right,
46
226537
4420
第四、五欄靠右都沒有更高的塔框住
03:50
so they can’t hold any energy.
47
230957
2470
所以,這兩欄都不能蓄積任何能量
03:53
We can adapt this idea into an algorithm.
48
233427
3818
這個概念可用一個演算法實踐:
03:57
Considering one column at a time as the point of reference,
49
237245
3780
凌杰每次以一欄塔作參照點
04:01
Hedge can look to the left stack by stack to find the height of the tallest one,
50
241025
4411
靠左,逐欄找最高一座塔的高度
04:05
look to the right to find the height of the tallest one,
51
245436
2720
又挨右,找最高一座塔的高度
04:08
and take the smaller of the two as the height the energy can fill up to.
52
248156
4677
兩者以較低者為蓄積後的平整高度
04:12
If the result is higher than the column in question,
53
252833
3130
結果若高於此欄本身的高度
04:15
subtract the height of the original column,
54
255963
2574
就減去原來的塔高
04:18
and the result will be the number of units that column can hold.
55
258537
5097
得出這座塔會蓄積到多少格能量
04:23
If it's equal to or below the level of the column in question,
56
263634
3560
相反,結果若少於或等於原來塔高
04:27
the energy would spill off.
57
267194
2203
能量就不會留住,往左右流走
04:29
Hedge can apply that to an entire basin with a loop
58
269397
3520
凌杰可用一個迴圈橫越整個能量池
04:32
that starts on the left-most column and moves right, one column at a time.
59
272917
5745
從最左一欄塔開始,每次一欄向右移
04:38
For each column, he’ll run the same steps— look all the way left for the tallest,
60
278662
5009
逐欄塔重複相同的幾個步驟:
先向左找最高一欄
04:43
do the same to the right, take the lower height of the two,
61
283671
3560
再向右找最高一欄
取二者較低者,減去原來欄高
04:47
subtract the original column height,
62
287231
2087
04:49
and increase the grand total if that number is positive.
63
289318
3860
若是正數,則加進蓄積格數的總計
04:53
His loop will repeat as many times as there are columns.
64
293178
3670
有多少欄,迴圈就重複多少次
04:56
That will work, but it’ll take a long time for a large basin.
65
296848
3950
這個方法雖然行得通
但能量池大的話就很費時
05:00
At every step Hedge repeats the action of looking left and looking right.
66
300798
4530
每個步驟凌杰都要向左、向右查看
05:05
If there are N stacks, he’ll look at all N stacks N times.
67
305328
4952
若有 N 座塔,就要看 N 次
05:10
Is there a faster way?
68
310280
1980
有更快的方法嗎?
05:12
Here’s one time saver: before doing anything else,
69
312260
3348
有一個省時妙計:一開始可先取巧
05:15
Hedge can start on the left,
70
315608
1860
凌杰從最左開始一邊數一邊做小抄
05:17
and keep a running tally of what the highest stack is.
71
317468
3870
以流水帳方式記下當前最高的欄高
05:21
Here that would be 2, 2 again, since the first was higher,
72
321338
3760
範例中,首欄是 2
然後也是 2,因為第一欄較高
05:25
then 4, 4, 4.
73
325098
2750
接著是 4、4、4
05:27
He can then find the highest right-most stacks
74
327848
2780
之後就輪到靠右的最高欄高
05:30
by doing the same going right-to-left: 1, 3, 4, 4, 4.
75
330628
6254
做法是從右往左重複一遍:
1、3、4、4、4
05:36
In the end he’ll have a table like this in his memory.
76
336882
3840
最後凌杰記憶中就有一個這樣的表
05:40
Now, Hedge can take one more pass to calculate how much energy there will be
77
340722
5239
現在凌杰只消多一重功夫
用先前同一條等式
05:45
above every stack with the same equation from before:
78
345961
4040
去計算每欄塔上可框住多少格能量:
05:50
take the smaller of the stored left and right values,
79
350001
3637
小抄中儲存的左值、右值取較低者
05:53
and subtract the height of the current tower.
80
353638
3070
減去該能量塔原來的高度
05:56
Instead of looking at N stacks N times, he’ll look at N stacks just 3 times—
81
356708
5585
N 座塔本來要看 N 次,現在只消 3 次
06:02
which is what’s called linear time.
82
362293
2280
即是所謂的線性時間
06:04
There are ways to optimize the solution even further,
83
364573
3241
這個解題方案還可以進一步優化
06:07
but this is good enough for our heroes.
84
367814
2750
但已可解燃眉之急了
06:10
Ethic and Hedge work as one.
85
370564
1770
彥霖、凌杰雙劍合璧
06:14
The first cascade is a breeze, and they rise up the tower.
86
374992
3844
第一關如沐春風,翻手之間就竄升
06:21
The second is a little tougher.
87
381573
2010
第二關稍有難度
06:33
The third is huge, with dozens of stacks of blocks.
88
393051
3860
第三關規模龐大,有數十座能量塔
06:36
The timer ticks down towards zero, but Ethic’s program is fast.
89
396911
4433
計時器快歸零,幸好編寫程式夠快
06:41
She gets the wheel in position just in time,
90
401344
2964
彥霖及時轉好輪盤
06:49
and the energy lifts them to the Node of Creation.
91
409015
2920
能量階梯把他們昇起,直達創造之泉
06:55
Like the first, it reveals a vision: memories of years gone by.
92
415640
5427
當一切如風,餘下也只有真相
哪似水流年的記憶再浮現眼前:
07:01
The world machine changed everything,
93
421067
2120
「創世機」翻天覆地打造世界大同
07:03
and Ethic, in her position as chief robotics engineer,
94
423187
3669
彥霖身為機器人總工程師看在眼裡
怵惕惻隱之心不禁戚戚
07:06
grew troubled by what she saw.
95
426856
2050
07:08
When the Bradbarrier went up to keep the people in,
96
428906
3040
當「八達圍」築起,把人圍堵城牆內
07:11
she knew something was seriously wrong.
97
431946
2640
專恣跋扈之甚 已離經叛道天地不容了
07:14
So she created three artifacts
98
434586
2090
彥霖決心撥亂反正,創造了三枚瑰寶
07:16
with the ability to restore people’s power, creativity, and memory,
99
436676
4545
讓力量、創造、記憶歸真,回歸人民
07:21
and smuggled them to three communities.
100
441221
2910
並暗中散落於三處社區,潛藏民間
07:24
Before she could tell people how to use them,
101
444131
2318
彥霖還來不及說出運用之法
07:26
the government discovered her efforts and sent bots to arrest her
102
446449
3510
政府就發現她的叛逆
派遣機器打造的土龍芻狗大肆搜捕
07:29
and the other programmers.
103
449959
1930
想把彥霖及其他程式設計師一網打盡
07:31
The last thing Ethic used the world machine to create
104
451889
3320
在最後關頭,彥霖再一次善用創世機
創造了一個機器人,囑託他至死守護
07:35
was a robot that would protect the ancient device
105
455209
2790
這台天地間的橐籥、造化萬物的玄機
07:37
from the forces of ignorance by enclosing it in a giant maze.
106
457999
4330
並埋藏巨型迷宮內 免於平庸無知之惡
07:42
She named her creation Hedge.
107
462329
2414
彥霖給了守護機器人一個名字:凌杰
07:51
Without warning, the energy lift flickers, then fizzles out.
108
471801
3830
能量階梯一閃消失,送上無情的迫降
關於本網站

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

https://forms.gle/WvT1wiN1qDtmnspy7