斯坦福大学开放课程——编程方法 作业1-4 (完美解决任何地图都能找到中点)【在中心点放置Beeper】

来源:互联网 发布:java实现加减乘除代码 编辑:程序博客网 时间:2024/05/16 09:47

下面的中文翻译是引用,出自:http://blog.csdn.net/goodpress/article/details/6170386

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、程序运行结束时卡雷尔的朝向不作要求。
解决这道题的算法很多,找出一个解决方法是本题的乐趣所在。

/*******************************引用结束****************************************/

/* * File: MidpointFindingKarel.java * ------------------------------- * When you finish writing it, the MidpointFindingKarel class should * leave a beeper on the corner closest to the center of 1st Street * (or either of the two central corners if 1st Street has an even * number of corners).  Karel can put down additional beepers as it * looks for the midpoint, but must pick them up again before it * stops.  The world may be of any size, but you are allowed to * assume that it is at least as tall as it is wide. */import stanford.karel.*;public class MidpointFindingKarel extends SuperKarel {/*我采用的策略是先放上一行的beeper然后从两端各拿掉一个。再下来从两端依次取一个直到取完为止最后根据karel的朝向决定放一个还是两个,测试在任何地图下均可。不足的地方望指出。谢谢!*/public void run(){fillOneRow();pickOneFromBothEnds();prepareForContinue(); // 为连续取创造条件,解决只有二,三道的情况while (beepersPresent()){ // 更多道则连续取,直到取完所有的beepermoveToEndPickBeeper();}accordingFacingPlacePutBeeper(); // 根据最后karel 面朝的方向和位置放置beeper}/* * 从两端各取一个,注意初始状态是 一个全被beeper填满 */private void pickOneFromBothEnds(){turnAround();if (beepersPresent()){pickBeeper();} moveToWall();turnAround();if (beepersPresent()){pickBeeper();}}/* * 为连续取做准备,这里还处理了只有两个道的情况 */private void prepareForContinue(){if (frontIsClear()){move();if (beepersPresent()){ // 处理两个道和三个道的情况pickBeeper();// 如果是三个道捡起中间那个beeper} else {// 两个道为了向西转身并前进一步turnAround();move();}if (frontIsClear()){ // 如果更多个道则向前一步为下一步连续取创造条件move();}}}/* * 移动到beeper链的另外一端转身并取一个然后前进不步保证karel始终站在链上 */private void moveToEndPickBeeper(){while (beepersPresent()){move();}turnAround(); // 这两行是因为前面那个循环最后是站在空白的位置上move();pickBeeper();move();}/* * 根据最后karel 面朝的方向和位置放置beeper,如果向东则宽度为奇数转身前一步放一个beeper,否则向西宽度为偶数 * 转身放一个beeper然后前进再放一个 */private void accordingFacingPlacePutBeeper(){if (facingEast()){turnAround();if (frontIsClear()){move();}putBeeper();} else {turnAround();putBeeper();if (frontIsClear()){move();}putBeeper();}}// 填满一行beeperpublic void fillOneRow(){while (frontIsClear()){putBeeper();move();}putBeeper();}// 移动到墙private void moveToWall(){while (frontIsClear()){move();}}}
/******************************************分割线**********************************************/

如果有更好的策略或方法希望让我知道,如果代码还可以优化也望指正。我将不胜感激。