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

430,906 views ・ 2020-02-27

TED-Ed


请双击下面的英文字幕来播放视频。

翻译人员: Sylvie Han 校对人员: Yolanda Zhang
00:31
Ethic and Hedge are on the ground floor of a massive tower.
0
31587
5701
Ethic 和 Hedge 站在巨大塔楼的底层。
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
为了拿到它, Ethic 要用三束能量流爬上塔楼。
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
Ethic 和 Hedge 必须确定 有多少能量会流下来。
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
这个结构能容纳 2 个单位的能量。
01:55
This configuration will capture 4— 3 here, and 1 here.
19
115618
5117
这个可以容纳 4 个单位—— 左边 3 个,右边 1 个。
02:00
And this one will also capture 4,
20
120735
2540
这个结构也可以容纳 4 个单位的能量,
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
Hedge 每次能让一个能量柱现形 并数出它的高度,
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
Ethic 要怎样让 Hedge 计算出
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
但 Hedge 一个个检查 这些单元格会耗费很多时间。
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
我们得出 3 格的结论,因为能量 最终会等于最高纵列——4,
03:32
and subtracting the height of the stack— so that’s 4 minus 1.
43
212186
4160
再减去这列的高度 —— 就是 4 减 1。
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
Hedge 由此向左一点点检查, 找到最高的纵列,
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
Hedge 可以对整个底座 依次这样操作,
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
每一步,Hedge 都要反复地左看右看。
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
首先,Hedge 从左边开始,
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
现在 Hedge 可以再扫描一次,
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 遍相比, 他只需把每个纵列看三遍——
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
Ethic 和 Hedge 配合默契。
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
计时器显示出 0, 但 Ethic 的动作很快。
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
Ethic 作为首席机器人工程师,
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
当 Brad 障碍升起并围住人群时,
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
Ethic 用世界机器创造的 最后一件物品是个机器人,
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
她给这个物品起名叫 Hedge。
07:51
Without warning, the energy lift flickers, then fizzles out.
108
471801
3830
在没有任何警告的情况下, 能量电梯开始闪烁,然后消散了。
关于本网站

这个网站将向你介绍对学习英语有用的YouTube视频。你将看到来自世界各地的一流教师教授的英语课程。双击每个视频页面上显示的英文字幕,即可从那里播放视频。字幕会随着视频的播放而同步滚动。如果你有任何意见或要求,请使用此联系表与我们联系。

https://forms.gle/WvT1wiN1qDtmnspy7