124k views
7 votes
(4 points.) Write an algorithm (i.e., step-by-step instructions) via which someone could walk or drive from some origin to some destination, perhaps a path that you yourself tend to travel. Take care to specify what the origin and destination that you have in mind. And take care to use loops and conditions as needed, particularly if there might be obstacles (e.g., red lines). Assume that your algorithm will be executed by a robot (or lost friend) who will follow your directions precisely, so avoid ALL ambiguity!

2 Answers

8 votes

Final answer:

The provided algorithm gives detailed instructions for a robot or person to navigate from a house at 5th Avenue and Main Street to a library on the same 5th Avenue and Oak Street in a hypothetical city. It includes steps for dealing with green and red traffic signals and unforeseen obstacles, ensuring successful two-dimensional motion within the city grid.

Step-by-step explanation:

Algorithm for Navigating a City with Square Blocks

Let's assume that our origin is a house located at the southwest corner of 5th Avenue and Main Street, and our destination is a library on the same 5th Avenue but at the intersection with Oak Street, six blocks to the north. Here is a step-by-step algorithm to walk from the origin to the destination in a city with a grid layout like New York, accounting for traffic signals (red lights) and obstacles:

  1. Begin at the southwest corner of 5th Avenue and Main Street.
  2. Walk north on 5th Avenue towards Oak Street.
  3. Loop
  4. : For each street intersection:
  5. If the traffic light is green, continue walking.
  6. If the traffic light is red, stop and wait for it to turn green before proceeding.
  7. Upon reaching the intersection with Oak Street, check for any obstacles:
  8. Arrive at the library, the destination.

This algorithm ensures that the person or robot following will navigate successfully, using two-dimensional motion, to the destination, while complying with traffic laws and being able to adapt to unforeseen obstacles.

User Jason Benson
by
3.8k points
8 votes

Answer:

The algorithm is as follows :

1. Record Current Position, Final Destination

2. While Current Position is not Final Destination, repeat the following:

2.1 If destination location is up then:

2.1.1 If an obstacle is directly above you then:

2.1.1.1 Look for alternate position (left, right, down)

2.1.1.2 If alternate position is found then

2.1.1.2.1 Move there

2.1.1.2.2 Record Current Position

2.1.1.3 If no alternate position is found then

2.1.1.3.1 Call for help

2.1.1.3.2 End

2.1.2 If no obstacle is directly above you then:

2.1.2.1 Move up

2.1.2.1 Record Current Position

2.2 If destination location is down then:

2.2.1 If an obstacle is directly below you then:

2.2.1.1 Look for alternate position (left, right, up)

2.2.1.2 If alternate position is found then

2.1.1.2.1 Move there

2.2.1.2.2 Record Current Position

2.2.1.3 If no alternate position is found then

2.2.1.3.1 Call for help

2.2.1.3.2 End

2.2.2 If no obstacle is directly below you then:

2.2.2.1 Move down

2.1.2.1 Record Current Position

2.3 If destination location is right then:

2.3.1 If an obstacle is directly by your right then:

2.3.1.1 Look for alternate position (down, up, left)

2.3.1.2 If alternate position is found then

2.3.1.2.1 Move there

2.3.1.2.2 Record Current Position

2.3.1.3 If no alternate position is found then

2.3.1.3.1 Call for help

2.3.1.3.2 End

2.3.2 If no obstacle is directly by your right then:

2.3.2.1 Move right

2.3.2.1 Record Current Position

2.4 If destination location is left then:

2.4.1 If an obstacle is directly by your left then:

2.4.1.1 Look for alternate position (down, up, right)

2.4.1.2 If alternate position is found then

2.4.1.2.1 Move there

2.4.1.2.2 Record Current Position

2.4.1.3 If no alternate position is found then

2.4.1.3.1 Call for help

2.4.1.3.2 End

2.4.2 If no obstacle is directly by your left then:

2.4.2.1 Move left

2.4.2.1 Record Current Position

Step-by-step explanation:

The algorithm is straight forward.

All it does is to check for available position and move into the position if there is any available.

It keeps checking and moving until an obstacle prevents it from moving.

User Barry Watts
by
3.6k points