[斯坦福开放课程.编程方法].作业.1-4.Karel将Beeper放在中间

    技术2024-10-18  59

    Assignments.1-4.MidpointFindingKarel.Programme Ethodology.Stanford Open Course.

    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.

     

     

    把Beeper放在Street 1的中央:/* * 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() { /*first 2 beepers*/ putBeeper(); while(frontIsClear()) { move(); } turnAround(); putBeeper(); /*put a beeper in the mid of th 1st street*/ while(beepersPresent()) { move(); while(noBeepersPresent()) { move(); } turnAround(); pickBeeper(); move(); if(noBeepersPresent()) { putBeeper(); }else { while(notFacingNorth()) { turnRight(); } move(); } } } } 把Beeper放在整个方形街区的中央:/* * 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 MidpointFindingKarelStreetAndAvenue extends SuperKarel { public void run() { /*first 2 beepers*/ putBeeper(); while(frontIsClear()) { move(); } turnAround(); putBeeper(); /*put a beeper in the mid of th 1st street*/ while(beepersPresent()) { move(); while(noBeepersPresent()) { move(); } turnAround(); pickBeeper(); move(); if(noBeepersPresent()) { putBeeper(); }else { while(notFacingNorth()) { turnRight(); } move(); } } /*put the beeper in the mid of avenue which based on the mid of th 1st street*/ /*put another basic beeper on the north of the avenue*/ while(frontIsClear()) { move(); } turnAround(); putBeeper(); /*serching the mid of the avenue(the same as searching the mid of the street)*/ while(beepersPresent()) { move(); while(noBeepersPresent()) { move(); } turnAround(); pickBeeper(); move(); if(noBeepersPresent()) { putBeeper(); }else { while(notFacingWest()) { turnRight(); } move(); } } } }  

     

     

    最新回复(0)