Algorithms: Solving a Maze POGIL Worksheet
Algorithms: Solving a Maze
The problem below is similar to a type of AP CSP exam question. Consider a robot that can follow the simple sequence commands below:
Let's put our robot in the maze below. The robot is represented as a black triangle and is initially facing up. It can only move forward to a white square. It cannot move onto the black squares or move beyond the edge of the grid.
Answer the following questions with your POGIL team:
CAN_MOVE(forward) will return true while CAN_MOVE(right) will return false.
MOVE_FORWARD
MOVE_FORWARD
ROTATE_RIGHT
MOVE_FORWARD
MOVE_FORWARD
MOVE_FORWARD
MOVE_FORWARD
ROTATE_LEFT
MOVE_FORWARD
MOVE_FORWARD
REPEAT n times
Commands
Rewrite your algorithm above using Repeat n times control structures (substituting in a
number for n) instead of repeating the MOVE_FORWARD command many times.
REPEAT 2 times
MOVE_FORWARD
ROTATE_RIGHT
REPEAT 4 times
MOVE_FORWARD
ROTATE_LEFT
REPEAT 2 times
MOVE_FORWARD
Here’s a more general way to navigate a maze with the following algorithm which uses GoalReached to test if the robot has reached the gray square.
REPEAT UNTIL GoalReached
{
IF (CAN_MOVE forward)
MOVE_FORWARD
IF (CAN_MOVE left)
ROTATE_LEFT
MOVE_FORWARD
IF (CAN_MOVE right)
ROTATE_RIGHT
MOVE_FORWARD
}
5.) (Portfolio) Write an algorithm for washing a stack of 10 items that are cups and saucers mixed together, where the rule is that the cups are washed in hot water and the saucers in cold water. Use simple commands like hot_wash and cold_wash. You may also use the control structures IF and REPEAT n times. Identify the parts of your algorithm that are examples of Sequence, Selection, and Repetition.
REPEAT UNTIL Goal Reached
{
IF (CUP)
HOT_WASH
IF (SAUCER)
COLD_WASH
}