The Train Heist | Think Like A Coder, Ep 4

533,259 views ・ 2019-12-09

TED-Ed


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

翻译人员: Carol Wang 校对人员: Jiasi Hao
[ 像程序员一样思考 ]
[ 地点:BRADBARRIER ]
[ 第四集 劫持火车 ]
00:21
Ethic, Hedge, and Adila, the leader of the revolution,
0
21570
3790
艾斯克、海吉和革命领袖埃迪拉
00:25
plot out how they can steal an artifact called the Node of Power.
1
25360
5179
制定了偷取神器“能量晶石”的方案。
00:30
It’s being used to run a heavily fortified train that runs all around the country,
2
30539
5557
它被用来运行重点防御的一列火车,
该专列负责为全国的定居点 和设施提供物资。
00:36
providing supplies to settlements and facilities.
3
36096
3730
00:39
This armored behemoth
4
39826
1628
这个庞然大物全副武装,
00:41
undergoes a complex and unpredictable unloading procedure—
5
41454
3988
其卸货过程 非常复杂且不可预测——
00:45
a procedure which is displayed, in detail, on a screen within the engine car.
6
45442
5384
该过程详细显示在 发动机车厢内的屏幕上。
00:50
Right means the train will go one car length forward,
7
50826
3560
向右的箭头表示 火车向前行驶一节车厢的距离,
00:54
and left means the train will go the same distance backwards.
8
54386
4001
而向左的箭头则表示 火车向后行驶相同的距离。
00:58
While unloading, the train frequently moves back and forth,
9
58387
3842
卸货期间,火车会不停地来回开动,
01:02
so a typical sequence might look like this.
10
62229
4960
因此,一个典型的移动顺序 可能是这样的。
01:07
Also within the engine car is a button that can only be pressed once.
11
67189
5085
此外,在发动机车厢内还有一个按钮, 该按钮只能按下一次。
01:12
When pressed, it lets down the force field over the artifact for 10 seconds.
12
72274
5390
按下后,将有 10 秒 解除对能量晶石的控制。
01:17
The engine car is tiny and designed for a robot.
13
77664
3750
发动机车厢很小,专为机器人设计。
01:21
Of your team, only Hedge can fit.
14
81414
3395
在你们的团队中,只有海吉进得去。
01:24
The members of the resistance have positioned a crane over the train tracks
15
84809
3908
抵抗组织成员已在车轨上方 安置了一台起重机,
01:28
that can pluck the artifact once it’s exposed.
16
88717
3167
能量晶石一旦显现, 即可以将其快速取出。
01:31
They’ll know when to lower the crane by sight.
17
91884
2995
他们能目测何时降下起重机,
01:34
But the only way Hedge can determine the train’s position
18
94879
3360
但是,因为海吉将置身于 无窗的发动机车厢内,
01:38
and know when to lower the force field
19
98239
2287
看不到车外情况,
01:40
is by analyzing the unloading procedure,
20
100526
3128
它只能通过分析卸货过程,
01:43
because he’ll be inside the windowless engine car.
21
103654
3968
才能确定火车的位置 并明确何时释放力场以解除晶石控制。
01:47
Hedge can’t program himself though, so it’s up to Ethic to tell him what to do.
22
107622
5376
海吉自己不懂编程, 所以需要艾斯克告诉它怎么做。
01:52
The artifact is in the car 10 positions behind
23
112998
3620
从起重机所在车厢处算起,
01:56
the one that’s directly under the crane at the start.
24
116618
3163
能量晶石在第 10 节车厢。
01:59
What instructions can Ethic give Hedge
25
119781
2704
艾斯克可以给予海吉什么指示,
02:02
so that he hits the button at just the right moment?
26
122485
3748
以便它在适当的时候按下按钮呢?
[ 5 秒后公布规则 ]
若想自己解题,可暂停播放。
[ 5 秒后公布规则 ]
02:13
Here’s a hint to get you started.
27
133506
3550
解题提示:
02:17
The key to this problem, as with many programming challenges,
28
137056
3857
与许多编程挑战一样,
02:20
is to reframe the information in a way that a computer can work with.
29
140913
4381
解题关键,就是用计算机 运行方式来重构信息。
02:25
A computer doesn’t know what a train is, nor does it need to.
30
145294
4620
计算机不知道火车是什么, 也不需要知道,
02:29
It can, however, work with variables.
31
149914
3110
但是,它可以使用变量。
02:33
Try making a variable that tracks the train’s position.
32
153024
4313
可尝试定义一个跟踪火车位置的变量,
02:37
How will it change as the train moves?
33
157337
2957
看看随着火车的移动 它会如何变化呢?
[ 5 秒后公布答案 ]
02:46
Let’s start by breaking this problem into two objectives.
34
166362
3650
就从将问题分为两个目标入手吧。
02:50
The first is to know where the train will be as it carries out its instructions.
35
170012
4873
首先,要确定火车执行卸货指示时的位置,
02:54
The second is to hit the button when the train is in just the right position.
36
174885
5184
其次,在火车处于正确位置时按下按钮。
03:00
For the first objective it’ll help to think of the train as a big number line.
37
180069
4669
对于第一个目标,把火车当做 一个大数轴理解就容易多了,
03:04
Let’s make 0 the car with the node, 1 the car in front of it, and so on.
38
184738
4854
把能量晶石所在的车厢设为 0, 它前面的车厢设为 1,依此类推,
03:09
That means car 10 is under the crane at the start.
39
189592
3690
得出车厢 10 位于起重机下方。
03:13
When the train moves one car right, car 9 is under the crane.
40
193282
4449
当火车向右移动一节车厢时, 车厢 9 位于起重机下方。
03:17
So a right arrow can be thought of as “subtract 1.”
41
197731
3810
因此,可将向右箭头视为“ - 1 ”。
03:21
And when the train moves left from there,
42
201541
2270
当火车再向左移动时, 车厢 10 将回到起重机下方,
03:23
10 is back under the crane,
43
203811
1840
03:25
making a left arrow the same as “add 1."
44
205651
3757
即向左箭头等同于“ + 1 ”。
03:29
Let’s set our train position variable to 10, since that’s where we start.
45
209408
4455
我们从起重机下方的车厢开始, 那应把火车位置变量设为 10 。
03:33
We can now use a loop to read the instructions one at a time,
46
213863
4330
我们现在用循环语句读取指示, 一次读一条指示,
03:38
adding or subtracting as we go, to track which car is under the crane.
47
218193
5104
边读边对位置变量进行加减运算,
得出起重机下的车厢编号。
03:43
The nice thing about setting up the variable this way
48
223297
2750
这样设置变量的好处是,
03:46
is that it tells us how far the node is from the crane.
49
226047
3316
它可以直接告诉我们 能量晶石距起重机有多远。
03:49
So as soon as the variable hits 0, Hedge should hit the button.
50
229363
4064
因此,只要变量等于 0, 海吉就应该按下按钮。
03:53
And here’s what happens.
51
233427
1810
下面就是实际操作。
03:55
Ethic gets into position on the crane while Hedge rushes off
52
235237
3674
在火车开始移动之前,
03:58
and slips into the engine car unnoticed, just before the train lurches to life.
53
238911
5898
海吉冲出去并滑入发动机车厢,
艾斯克进入起重机就位。
04:04
It rolls 3 cars back. 1 forward, another 4 back.
54
244809
4893
火车先后退 3 节车厢, 又前进 1 节,再后退 4 节,
04:09
Then so far forward Ethic loses track before it reverses once more.
55
249702
5819
此时,艾斯克已茫然不知 起重机下是哪节车厢。
04:15
When the artifact finally rolls into position,
56
255521
3021
当能量晶石终于处于起重机正下方时,
04:18
Adila lowers the crane, hoping Ethic and Hedge got it right.
57
258542
3790
埃迪拉放下起重机,
并希望艾斯克和海吉能操作无误,
04:22
At the last possible moment, the force field sputters and falls.
58
262332
4372
在最后可能的时刻, 力场轰然崩塌。
04:26
Ethic swoops in, and lifts the Node of Power to freedom.
59
266704
5011
艾斯克冲过去, 成功拿到了能量晶石。
04:31
When Ethic gives the node to Hedge for safe keeping,
60
271715
2910
当艾斯克把它交给海吉安全保管时,
04:34
something incredible happens.
61
274625
2091
不可思议的事情发生了。
04:36
The artifact shimmers to life with a vision of the past:
62
276716
3850
能量晶石闪烁的光芒中 闪现出过去发生的一幕幕:
04:40
When the crystal was unearthed, no one could make the console inside work.
63
280566
4806
水晶刚出土的时候, 没人能使其内部控制台工作。
04:45
The government put out a call for people to try their luck with it,
64
285372
3250
政府呼吁群众都来试试运气,
04:48
one at a time.
65
288622
1452
每个人试一次。
04:50
Ethic loved to figure out what made things tick, so she signed up.
66
290074
4323
艾斯克想弄清水晶滴答作响的原委, 就报名了。
04:54
Within moments at the console, something clicked into place,
67
294397
3143
她登上控制台的片刻间, 一片声音响起,又回归安静。
04:57
and she created her first robot.
68
297540
2760
她就这样创造了自己的第一个机器人。
05:00
The government hired Ethic as chief robotics engineer on the spot.
69
300300
5014
政府当场就聘请艾斯克 为首席机器人工程师。
05:05
Within a year her creations ran almost every aspect of society,
70
305314
4170
在不到一年的时间里, 她创造的机器人几乎遍及各个角落,
05:09
and the nation and its people thrived,
71
309484
2149
国家及人民繁荣昌盛,
05:11
no longer needing to toil in the fields and factories.
72
311633
3475
不用再在工厂和田野里劳作。
05:21
The vision ends, and Hedge detects the second artifact in the 198forest,
73
321047
6763
历史影像播放结束, 海吉发现第二件神器在 198 森林,
05:27
to the southeast.
74
327810
1460
东南方。
05:29
Luckily, the train is going there next,
75
329270
2336
幸运的是,火车接下来就去那里,
05:31
and has just enough reserve fuel for the trip.
76
331606
3034
并且有足够燃料储备抵达。
05:34
Ethic and Hedge smuggle themselves aboard
77
334640
2557
艾斯克和海吉悄悄登车,
05:37
and find a hiding spot for the long journey ahead.
78
337197
3558
为接下来的漫长旅程 找到了藏身之所。
关于本网站

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

https://forms.gle/WvT1wiN1qDtmnspy7