习题要求:
下面的中文翻译是引用,出自: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 {
/**
* 首先放满一排方块,然后循环每次从两边各拿掉一个方块,直到最后只剩一格或者两格有方块。当然中间没有用到任何计数器
*/
public void run() {
filledTheRow();
while (!isEnd()) {//如果前面不是边界,并且后面2步内也不是边界,则将两头的方块捡起来
while (beepersPresent()) {
pickBeeper();
}
turnAround();
moveToBounds();
while (beepersPresent()) {
pickBeeper();
}
moveBack();
}
}
private void leaveOneBeeper() {
while(beepersPresent()) pickBeeper();
putBeeper();
}
/**
* 移动到最后一个有方块的点上
*/
private void moveToBounds() {
while (frontIsClear()) {
move();
if (noBeepersPresent()) {
moveBack();
break;
}
}
}
/**
* @return
*/
private boolean isEnd() {
return isBounds() && backIsBounds();
}
/**
* 判断是否到达边界
*
* @return 前面是墙或者前面没有方块时返回true,否则返回false
*/
private boolean isBounds() {
boolean isBounds = false;
if (frontIsBlocked()){
isBounds = true;
} else {
move();
if (noBeepersPresent())
isBounds = true;
moveBack();
}
return isBounds;
}
/**
* 判断后面是否到达边界,判断完成后,位置保持不变
*
* @return 如果后面是墙或者没有方块,或者后退一步后后面是墙或者没有方块,则返回true,否则返回false
*/
private boolean backIsBounds() {
boolean backIsBounds = false;
turnAround();
if (frontIsBlocked()) {
backIsBounds = true;
} else {
move();
backIsBounds = (noBeepersPresent() || isBounds());
moveBack();
}
turnAround();
return backIsBounds;
}
/**
* 后退一步,面向方向不变
*/
private void moveBack() {
turnAround();
move();
turnAround();
}
/**
* 放满一排方块
*/
private void filledTheRow() {
while (true) {
putBeeper();
if (frontIsBlocked())
break;
move();
}
}
}