斯坦福大学开放课程——编程方法 作业1-4 (附作业中文翻译及源码)【在中心点放置Beeper】

Problem 4

As an exercise in solving algorithmic problems, program Karel to place a single beeper at the center of 1st Street. For example, if Karel starts in the world

it should end with Karel standing on a beeper in the following position:

Note that the final configuration  of the world should have only a single beeper at the midpoint of 1st Street.  Along the way, Karel is allowed to place additional beepers wherever it wants to, but must pick them all up again before it finishes.

In solving this problem, you may count on the following facts about the world:

• Karel starts at 1st Avenue and 1st Street, facing east, with  an infinite number of beepers in its bag.
• The initial state of the world includes no interior walls or beepers.
• The world need not be square, but you may assume that it is at least as tall as it is wide.

Your program, moreover, can assume the following simplifications:

• If the width of the world is odd, Karel must put the beeper in the center square. If the width is even, Karel may drop the beeper on either of the two center squares.
• It does not matter which direction Karel is facing at the end of the run.

There are many different algorithms you can use to solve this problem. The interesting part of this assignment is to come up with a strategy that works.

****************翻译分割线***************

1、卡雷尔的起始位置是第一列第一行，面朝东，携带了无限的灰色方块；
2、界面在初始状态下没有任何内墙或灰色方块；
3、界面不一定是正方形，但你可以假设它的长宽相等。

1、如果界面的宽是奇数，卡雷尔必须把灰色方块放置在中间的位置；如果是偶数，卡雷尔可以把灰色方块放在中间的两个位置中的任意一个。
2、程序运行结束时卡雷尔的朝向不作要求。

**************分割线*****************