(点击右边的三个点,可以下载音频和调整播放速度)
Dawn and the train are both breaking when Ethic and Hedge arrive in the woods.
( 像程序员一样思考 )(地点:198 树林 ) ( 第五集 艺术家们 ) 破晓时分,火车停下, 艾斯克和海吉抵达了树林。
The adventurers have recovered the first artifact— the Node of Power— and have come to the 198forest in search of the second.
这两位冒险者 已找到第一件神器 —— 能量晶石, 现抵达 198 森林, 来找寻第二件神器,
Here they're welcomed by the director of the colony, Octavia.
殖民首领奥克塔维亚 欢迎他们的到来。
She established this treehouse sanctuary after the robots freed everyone from having to work.
机器人把人们从工作中解放后, 她建立了这个树屋保护区。
It was meant to be a haven where people could follow their passions, take up crafts, and find fulfillment.
这本应是人们追随自己激情、 从事手工业,获得成就感的港湾,
Which they did… at first.
一开始,人们也确实是这么做的,
Some years ago everyone forgot the point.
但几年前,大家都忘记了初衷,
They abandoned arts and crafts and instead just painted and exhibited pictures of themselves over, and over, and over.
他们放弃了艺术和手工, 只是不停地画自己, 并展示自己的照片, 日复一日,年复一年。
The location of the second artifact is no secret; it's in a tower, guarded by a garrison of bots, a bottomless ravine,
第二件神器的位置众所周知, 位于一个万丈深谷的塔里, 由机器人看守,
and who knows what other traps.
有众多不为人知的陷阱。
As soon as the tower went up with the node inside, human communication across the land went dark.
藏有能量晶石的塔高耸入云, 整个地球的人类交流 在该塔建成后从此中断。
Octavia's been after it for years, but try as she might, the defenses thwart her.
奥克塔维亚关注它好几年了, 但无论她怎么尝试, 都无法突破其防御系统。
In order to even get to the tower, the team will need a distraction.
艾斯克团队为到达塔下, 需要分散守卫的注意力。
Octavia has an idea: stir up the people through some well-intentioned vandalism.
奥克塔维亚有个主意:通过一些善意的破坏公物行为, 来煽动人们(从而转移守卫视线)。
The residents' paintings are all squares that come in different sizes, all an odd number of pixels across.
居民画像是大小不一的正方形, 每个方向的像素数都是奇数。
Helper-bots pick up the finished portraits and hang them in public places for everyone to admire.
机器人助手收集画完的肖像, 并挂在公共场所供大家欣赏。
There's a slim margin of time when Hedge can access the paintings.
海吉只有极短的时间窗口 能接触到这些画,
If he were to deface each one with an X, the people would blame the helper-bots, creating just the distraction the team needs.
如果它在每幅画上涂个 X, 人们就会责怪机器人助手,从而实现他们 分散机器人注意力的目的,
If only it were so easy.
要是实现起来 也如此容易就好了,
Hedge can't just paint an X— his painting processor requires very specific instructions.
海吉不是如此简单就能画上 X, 它的绘画处理器 需要非常具体的指令。
Treating the paintings as square grids, he can fill in one pixel, or little square, at a time.
它把这些画当作正方形网格, 一次只能填一个像素, 即只能填一个小正方形。
He can move forwards and make 90 degree turns over the canvas, but can't move diagonally.
它在画布上可以向前移动, 也能进行 90 度旋转,但不能斜着移动。
How does Ethic program Hedge to paint an X over each portrait?
艾斯克如何编程, 才能让海吉在自画像上涂上 X 呢?
Pause now to figure it out for yourself.
可暂停播放自行解题
Here's a hint.
思路如下:
Try drawing a square grid like this, and simulating Hedge's path over it.
先画一个正方形网格,如图, 然后模拟海吉的涂画路径。
What patterns can you find to guide him?
如何指导海吉完成任务呢?
Pause now to figure it out for yourself.
现在尝试一下自己弄清楚。
The challenge here is to craft a set of instructions that will work for any square grid.
这里的挑战是如何设计指令组, 以适用任何大小的正方形网格。
Fortunately, one of the strengths of programming is the flexibility to solve not just one problem, but a whole class of them all at once.
幸运的是,编程的优点之一 就是其灵活性, 它并非只能解决一个问题, 而是一次性解决一类问题。
It often helps to start with one case, and work towards the general.
先从一个案例开始着手, 总有助于找到整体方案。
Let's say we had this square.
我们假设正方形如图所示,
Hedge can measure the length of its sides and store that number as a variable.
海吉可测量其边长, 并保存为变量值。
Now, what we need is a plan for how Hedge will paint an X, pixel by pixel.
现在,我们需要一个计划, 让海吉把像素逐个画上 X 。
There's more than one right answer for how to do this; let's look at two.
实现方法不止一个, 我们先来看两个方法。
First, what if Hedge went row by row, like a typewriter?
第一种,打字机模式, 让海吉一行一行地画。
If it's a 9 pixel by 9 pixel painting, in the first row he'd paint, skip 7, and then paint again.
如果画像是 9 X 9 像素, 它先画第一行第一格, 跳过七格,然后画一格,
In the second row he'd skip the first, paint, skip 5, and paint.
第二行,他跳过第一格再画, 跳过五格再画, 继续画下一行。
And so on.
等
The pattern here is that for each row the pixels skipped at the beginning go up by one, and the pixels skipped in the middle go down by 2.
规则是,每行开始跳过的像素 +1, 中间跳过的像素 -2,
Things get more complicated when Hedge reaches the center.
海吉画到中心点时, 情况变得更加复杂。
Here there's a row with just one pixel painted.
这一行只画一个像素。
Then the whole thing reverses— the number of pixels skipped goes down by one each time on the left, and up by two each time in the middle.
然后,画的顺序就全反了 —— 左边跳过的像素数每行 -1, 每行中间跳过的像素数 +2。
Instructing Hedge to do this with a series of loops will work and is a perfectly fine solution.
海吉执行这组循环指令是可行的, 且是完美解决方案。
The main drawback is that this requires quite a bit of logic— knowing what to do in the middle, when to reverse the process,
主要缺点是其中涉及的逻辑很多—— 每行中间做什么、何时反向操作,
and exactly how to reverse it.
以及如何精确反向操作。
So how might we approach this so that the logic remains consistent from start to finish?
那么,我们如何操作, 才能使逻辑始终一致呢?
The key insight is to look at a grid as a series of concentric squares.
关键是把网格 看作是一系列的同心正方形。
Each square follows the same pattern— painted pixels in the corners, and unaltered pixels in between.
每个正方形遵循相同模式—— 只画四个角的像素, 其余像素不变。
So if we can figure out a way to paint one nested square, transition to the next, and repeat, we can paint them all.
因此,如果我们能找到 绘制一个嵌套正方形的方法, 即可应用到下一个,不断重复, 就能把它们全部绘制出来。
Painting the outermost one is easy.
最外层正方形最容易绘制。
Start in a corner and paint that pixel.
从一角开始,先画该角的像素。
If we call the length of the painting n, fly forward n minus 1 spaces.
假设画的长度为 n 像素, 就向前移动 n - 1 格,
Paint another pixel, and turn right.
再画一个像素,然后右转。
Now do the whole thing again… and again.
接着再做一次循环操作 , 接着再做一次,
Now move forward one less space, turn right, fly forward once, and Hedge will be in the next concentric square and ready to repeat the whole process.
现在向前少移动一格, 右转,再向前移动一格, 海吉正好位于 下个同心正方形一角, 即可重复下一个循环过程。
Each square is n minus 2 pixels smaller than the last in length and width,
每个正方形的长宽 都比上一个少 n - 2 像素,
and we can follow this spiral pattern all the way to the center with a loop and a variable that tracks how far Hedge should fly.
继续该螺旋模式, 通过变量和循环控制海吉的步伐, 最终一直画到中心点。
Is one of these methods better than the other?
两种方式哪种更优呢?
It really depends on what you value.
这取决于你的侧重点。
The strength of the spiral is the simplicity of finding a pattern and reusing the same logic from start to finish.
螺旋循环的优点在于, 它简单地找到一个模式, 并从头到尾循序相同逻辑。
The advantage of the typewriter approach is that it's a more generalized solution, meaning it can be adapted much more simply to fill in any pattern.
打字机方法的优点在于, 它是一个更通用的解决方案, 即它容易适应任何模式。
For Ethic's sake, either will do just fine.
对艾斯克来说,哪种都能达成目的。
So here's what happens.
所以结果是这样的,
Hedge rapidly defaces all of the portraits.
海吉很快弄脏了所有的肖像,
And within moments cries of anguish break out all over the forest.
不一会儿, 痛苦的哭声响彻整个森林。
The garrison guarding the tower abandon their posts to calm the agitated people, and Ethic, Hedge,
守卫塔的守卫们 离开了他们的岗位, 以安抚骚动的人们,
and Octavia slip through— and nearly slip into the depths of the gorge standing between them and the tower.
艾斯克、海吉和奥克塔维亚 悄悄穿了过去 —— 差一点掉进幽幽深谷, 那位于他们和塔之间的万丈峡谷。
|
|