The Artists | Think Like A Coder, Ep 5

436,291 views ・ 2020-01-13

TED-Ed


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

翻译人员: Carol Wang 校对人员: Jiasi Hao
[ 像程序员一样思考 ]
[ 地点:198 树林 ]
[ 第五集 艺术家们 ]
00:22
Dawn and the train are both breaking when Ethic and Hedge arrive in the woods.
0
22840
5488
破晓时分,火车停下, 艾斯克和海吉抵达了树林。
00:28
The adventurers have recovered the first artifact—
1
28328
2880
这两位冒险者 已找到第一件神器 ——
00:31
the Node of Power—
2
31208
1497
能量晶石,
00:32
and have come to the 198forest in search of the second.
3
32705
4030
现抵达 198 森林, 来找寻第二件神器,
00:36
Here they’re welcomed by the director of the colony, Octavia.
4
36735
5111
殖民首领奥克塔维亚 欢迎他们的到来。
00:41
She established this treehouse sanctuary
5
41846
2600
机器人把人们从工作中解放后,
00:44
after the robots freed everyone from having to work.
6
44446
2969
她建立了这个树屋保护区。
00:47
It was meant to be a haven where people could follow their passions,
7
47415
3290
这本应是人们追随自己激情、
00:50
take up crafts, and find fulfillment.
8
50705
2370
从事手工业,获得成就感的港湾,
00:53
Which they did… at first.
9
53075
2263
一开始,人们也确实是这么做的,
00:55
Some years ago everyone forgot the point.
10
55338
2510
但几年前,大家都忘记了初衷,
00:57
They abandoned arts and crafts
11
57848
1630
他们放弃了艺术和手工,
00:59
and instead just painted and exhibited pictures of themselves
12
59478
3200
只是不停地画自己, 并展示自己的照片,
01:02
over, and over, and over.
13
62678
2600
日复一日,年复一年。
01:05
The location of the second artifact is no secret;
14
65278
2610
第二件神器的位置众所周知,
01:07
it’s in a tower, guarded by a garrison of bots,
15
67888
3460
位于一个万丈深谷的塔里,
由机器人看守, 有众多不为人知的陷阱。
01:11
a bottomless ravine, and who knows what other traps.
16
71348
4150
01:15
As soon as the tower went up with the node inside,
17
75498
3158
藏有能量晶石的塔高耸入云,
01:18
human communication across the land went dark.
18
78656
3922
整个地球的人类交流 在该塔建成后从此中断。
01:22
Octavia’s been after it for years,
19
82578
2050
奥克塔维亚关注它好几年了,
01:24
but try as she might, the defenses thwart her.
20
84628
4245
但无论她怎么尝试, 都无法突破其防御系统。
01:28
In order to even get to the tower, the team will need a distraction.
21
88873
4258
艾斯克团队为到达塔下, 需要分散守卫的注意力。
01:33
Octavia has an idea:
22
93131
1849
奥克塔维亚有个主意:
01:34
stir up the people through some well-intentioned vandalism.
23
94980
4131
通过一些善意的破坏公物行为, 来煽动人们(从而转移守卫视线)。
01:39
The residents’ paintings are all squares that come in different sizes,
24
99111
3832
居民画像是大小不一的正方形,
01:42
all an odd number of pixels across.
25
102943
3608
每个方向的像素数都是奇数。
01:46
Helper-bots pick up the finished portraits
26
106551
2412
机器人助手收集画完的肖像,
01:48
and hang them in public places for everyone to admire.
27
108963
3848
并挂在公共场所供大家欣赏。
01:52
There’s a slim margin of time when Hedge can access the paintings.
28
112811
4190
海吉只有极短的时间窗口 能接触到这些画,
如果它在每幅画上涂个 X,
01:57
If he were to deface each one with an X,
29
117001
3361
02:00
the people would blame the helper-bots,
30
120362
2439
人们就会责怪机器人助手,
02:02
creating just the distraction the team needs.
31
122801
3410
从而实现他们 分散机器人注意力的目的,
02:06
If only it were so easy.
32
126211
2300
要是实现起来 也如此容易就好了,
02:08
Hedge can’t just paint an X—
33
128511
2230
海吉不是如此简单就能画上 X,
02:10
his painting processor requires very specific instructions.
34
130741
4121
它的绘画处理器 需要非常具体的指令。
02:14
Treating the paintings as square grids,
35
134862
2630
它把这些画当作正方形网格,
02:17
he can fill in one pixel, or little square, at a time.
36
137492
4000
一次只能填一个像素, 即只能填一个小正方形。
02:21
He can move forwards and make 90 degree turns over the canvas,
37
141492
3720
它在画布上可以向前移动, 也能进行 90 度旋转,
02:25
but can’t move diagonally.
38
145212
2728
但不能斜着移动。
02:27
How does Ethic program Hedge to paint an X over each portrait?
39
147940
4802
艾斯克如何编程, 才能让海吉在自画像上涂上 X 呢?
02:32
Pause now to figure it out for yourself.
40
152742
8528
[ 可暂停播放自行解题 ]
[ 涂画规则 4 ]
[ 涂画规则 3 ]
[ 涂画规则 2 ]
[ 涂画规则 1 ]
[ 思路 5 ]
[ 思路 4 ]
[ 思路 3 ]
[ 思路 2 ]
02:41
Here’s a hint.
41
161270
1530
思路如下:
02:42
Try drawing a square grid like this,
42
162800
2702
先画一个正方形网格,如图,
02:45
and simulating Hedge’s path over it.
43
165502
2950
然后模拟海吉的涂画路径。
02:48
What patterns can you find to guide him?
44
168452
2556
如何指导海吉完成任务呢?
[ 按暂停自己解题 ]
02:51
Pause now to figure it out for yourself.
45
171008
4790
[ 答案公布 3 ]
[ 答案公布 2 ]
[ 答案公布 1 ]
02:55
The challenge here is to craft a set of instructions
46
175798
2599
这里的挑战是如何设计指令组,
02:58
that will work for any square grid.
47
178397
2791
以适用任何大小的正方形网格。
03:01
Fortunately, one of the strengths of programming
48
181188
2570
幸运的是,编程的优点之一 就是其灵活性,
03:03
is the flexibility to solve not just one problem,
49
183758
2980
它并非只能解决一个问题,
03:06
but a whole class of them all at once.
50
186738
3110
而是一次性解决一类问题。
03:09
It often helps to start with one case, and work towards the general.
51
189848
4801
先从一个案例开始着手, 总有助于找到整体方案。
03:14
Let’s say we had this square.
52
194649
1970
我们假设正方形如图所示,
03:16
Hedge can measure the length of its sides and store that number as a variable.
53
196619
4479
海吉可测量其边长, 并保存为变量值。
03:21
Now, what we need is a plan for how Hedge will paint an X,
54
201098
4585
现在,我们需要一个计划, 让海吉把像素逐个画上 X 。
03:25
pixel by pixel.
55
205683
1540
03:27
There’s more than one right answer for how to do this;
56
207223
2952
实现方法不止一个,
我们先来看两个方法。
03:30
let’s look at two.
57
210175
1483
03:31
First, what if Hedge went row by row, like a typewriter?
58
211658
3638
第一种,打字机模式, 让海吉一行一行地画。
03:35
If it’s a 9 pixel by 9 pixel painting,
59
215296
3023
如果画像是 9 X 9 像素,
03:38
in the first row he’d paint, skip 7, and then paint again.
60
218319
4309
它先画第一行第一格, 跳过七格,然后画一格,
03:42
In the second row he’d skip the first, paint, skip 5, and paint.
61
222628
4818
第二行,他跳过第一格再画, 跳过五格再画,
03:47
And so on.
62
227446
1330
继续画下一行。
03:48
The pattern here is that for each row the pixels skipped at the beginning
63
228776
4242
规则是,每行开始跳过的像素 +1,
03:53
go up by one,
64
233018
1410
03:54
and the pixels skipped in the middle go down by 2.
65
234428
3650
中间跳过的像素 -2,
海吉画到中心点时, 情况变得更加复杂。
03:58
Things get more complicated when Hedge reaches the center.
66
238078
3080
04:01
Here there’s a row with just one pixel painted.
67
241158
3041
这一行只画一个像素。
04:04
Then the whole thing reverses—
68
244199
1969
然后,画的顺序就全反了 ——
04:06
the number of pixels skipped goes down by one each time on the left,
69
246168
4123
左边跳过的像素数每行 -1,
04:10
and up by two each time in the middle.
70
250291
3366
每行中间跳过的像素数 +2。
04:13
Instructing Hedge to do this with a series of loops will work
71
253657
3454
海吉执行这组循环指令是可行的,
04:17
and is a perfectly fine solution.
72
257111
2439
且是完美解决方案。
04:19
The main drawback is that this requires quite a bit of logic—
73
259550
3601
主要缺点是其中涉及的逻辑很多——
04:23
knowing what to do in the middle, when to reverse the process,
74
263151
3020
每行中间做什么、何时反向操作,
04:26
and exactly how to reverse it.
75
266171
2690
以及如何精确反向操作。
04:28
So how might we approach this
76
268861
1784
那么,我们如何操作,
04:30
so that the logic remains consistent from start to finish?
77
270645
3916
才能使逻辑始终一致呢?
04:34
The key insight is to look at a grid as a series of concentric squares.
78
274561
5488
关键是把网格 看作是一系列的同心正方形。
每个正方形遵循相同模式——
04:40
Each square follows the same pattern—
79
280049
2640
04:42
painted pixels in the corners, and unaltered pixels in between.
80
282689
4222
只画四个角的像素, 其余像素不变。
04:46
So if we can figure out a way to paint one nested square,
81
286911
3418
因此,如果我们能找到 绘制一个嵌套正方形的方法,
04:50
transition to the next, and repeat, we can paint them all.
82
290329
4435
即可应用到下一个,不断重复, 就能把它们全部绘制出来。
04:54
Painting the outermost one is easy.
83
294764
2361
最外层正方形最容易绘制。
04:57
Start in a corner and paint that pixel.
84
297125
2489
从一角开始,先画该角的像素。
04:59
If we call the length of the painting n,
85
299614
2440
假设画的长度为 n 像素,
就向前移动 n - 1 格,
05:02
fly forward n minus 1 spaces.
86
302054
2750
05:04
Paint another pixel, and turn right.
87
304804
2500
再画一个像素,然后右转。
05:07
Now do the whole thing again… and again.
88
307304
3530
接着再做一次循环操作 , 接着再做一次,
05:10
Now move forward one less space, turn right, fly forward once,
89
310834
5195
现在向前少移动一格, 右转,再向前移动一格,
海吉正好位于 下个同心正方形一角,
05:16
and Hedge will be in the next concentric square
90
316029
3170
05:19
and ready to repeat the whole process.
91
319199
2785
即可重复下一个循环过程。
05:21
Each square is n minus 2 pixels smaller than the last in length and width,
92
321984
5742
每个正方形的长宽 都比上一个少 n - 2 像素,
05:27
and we can follow this spiral pattern all the way to the center
93
327726
3920
继续该螺旋模式,
通过变量和循环控制海吉的步伐, 最终一直画到中心点。
05:31
with a loop and a variable that tracks how far Hedge should fly.
94
331646
5147
05:36
Is one of these methods better than the other?
95
336793
2240
两种方式哪种更优呢?
这取决于你的侧重点。
05:39
It really depends on what you value.
96
339033
2064
05:41
The strength of the spiral is the simplicity of finding a pattern
97
341097
3430
螺旋循环的优点在于, 它简单地找到一个模式,
05:44
and reusing the same logic from start to finish.
98
344527
3040
并从头到尾循序相同逻辑。
05:47
The advantage of the typewriter approach
99
347567
2100
打字机方法的优点在于,
05:49
is that it’s a more generalized solution,
100
349667
2290
它是一个更通用的解决方案,
05:51
meaning it can be adapted much more simply to fill in any pattern.
101
351957
4070
即它容易适应任何模式。
对艾斯克来说,哪种都能达成目的。
05:56
For Ethic’s sake, either will do just fine.
102
356027
3271
05:59
So here’s what happens.
103
359298
1830
所以结果是这样的,
海吉很快弄脏了所有的肖像,
06:01
Hedge rapidly defaces all of the portraits.
104
361128
3109
06:04
And within moments
105
364237
1110
不一会儿,
06:05
cries of anguish break out all over the forest.
106
365347
3671
痛苦的哭声响彻整个森林。
守卫塔的守卫们 离开了他们的岗位,
06:09
The garrison guarding the tower abandon their posts
107
369018
2980
06:11
to calm the agitated people,
108
371998
1970
以安抚骚动的人们,
06:13
and Ethic, Hedge, and Octavia slip through—
109
373968
2990
艾斯克、海吉和奥克塔维亚 悄悄穿了过去 ——
06:16
and nearly slip into the depths of the gorge
110
376958
3040
差一点掉进幽幽深谷,
06:19
standing between them and the tower.
111
379998
2889
那位于他们和塔之间的万丈峡谷。
关于本网站

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

https://forms.gle/WvT1wiN1qDtmnspy7