SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 1 Day Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 20 Dec 2017 My Price 8.00

Assume that a maze is a rectangular array of squares,

. Do you know how to find your way through a maze? After you write this program, you will never be lost again!

 Assume that a maze is a rectangular array of squares, some of which are blocked to represent walls. The

maze has one entrance and one exit. For example, if x ’s represent the walls, a maze could appear as follows:

 

 xxxxxxxxxxxxxxxxxx x

 x           x                 xxxx x

 x xxxxx       xxxxx xx x

 x xxxxx  xxxxxxx xx x

 x x                        xx  xx  x

 x xxxxxxxxxx  xx        x

 xxxxxxxxxxxx o xxxxxxx

 

 A creature, indicated in the previous diagram by o , sits just inside the maze at the entrance (bottom row).

Assume that the creature can move in only four directions: north, south, east, and west. In the diagram, north is

up, south is down, east is to the right, and west is to the left. The problem is to move the creature through the

maze from the entrance to the exit (top row), if possible. As the creature moves, it should mark its path. At the

conclusion of the trip through the maze, you should see both the correct path and incorrect attempts. Write a

program to solve this problem.

 

 

Squares in the maze have one of several states: CLEAR (the square is clear), WALL (the square is blocked

and represents part of the wall), PATH (the square lies on the path to the exit), and VISITED (the square was

visited, but going that way led to an impasse).

 This problem uses two ADTs that must interact. The ADT creature represents the creature’s current position

and contains operations that move the creature. The creature should be able to move north, south, east, and west

one square at a time. It should also be able to report its position and mark its trail.

 The ADT maze represents the maze itself, which is a two-dimensional rectangular arrangement of squares.

You could number the rows of squares from the top beginning with zero, and number the columns of squares

from the left beginning with zero. You could then use a row number and a column number to uniquely identify

any square within the maze. The ADT clearly needs a data structure to represent the maze. It also needs such data

as the height and width of the maze given in numbers of squares; the length of a side of a square, and the row and

column coordinates of both the entrance to and the exit from the maze.

 The ADT maze should also contain, for example, operations that create a specific maze given descriptive

data that we will detail to display a maze, determine whether a particular square is part of the wall, determine

whether a particular square is part of the path, and so on.

 The search algorithm and its supporting functions are outside both of the ADTs creature and maze. Thus,

the maze and the creature will be arguments that you must pass to these functions. If you are at the maze’s

entrance, you can systematically find your way out of the maze by using the following search algorithm. This

involves backtracking—that is, retracing your steps when you reach an impasse.

 

 Step 1. First check whether you are at the exit. If you are, you’re done (a very simple maze); if you are not,

go to step 2.

 Step 2. Try to move to the square directly to the north by calling the function goNorth (step 3).

 Step 3. If goNorth was successful, you are done. If it was unsuccessful, try to move to the square directly

to the west by calling the function goWest (step 4).

 Step 4. If goWest was successful, you are done. If it was unsuccessful, try to move to the square directly to

the south by calling the function goSouth (step 5).

 Step 5. If goSouth was successful, you are done. If it was unsuccessful, try to move to the square directly

to the east by calling the function goEast (step 6).

 Step 6. If goEast was successful, you are done. If it was unsuccessful, you are still done, because no path

exists from the entrance to the exit.

 

 The function goNorth will examine all the paths that start at the square to the north of the present square

as follows. If the square directly to the north is clear, is inside the maze, and has not been visited before,

move into this square and mark it as part of the path. (Note that you are moving from the south.) Check

whether you are at the exit. If you are, you’re done. Otherwise, try to find a path to the exit from here by try-

ing all paths leaving this square except the one going south (going south would put you back in the square

from which you just came) as follows. Call goNorth; if it is not successful, call goWest and, if it is not suc-

cessful, call goEast. If goEast is not successful, mark this square as visited, move back into the square to the

south, and return.

 

 The following pseudocode describes the goNorth algorithm:

goNorth(maze, creature)

     if (the square to the north is clear, inside the maze, and unvisited )

{

         Move to the north

          Mark the square as part of the path

         if (at exit)

 success = true

 else

{

              success = goNorth(maze, creature)

               if (!success)

              {

                    success = goWest(maze, creature)

             if (!success)

            {

                success = goEast(maze, creature)

                 if (!success)

                {

                     Mark square visited

                     Backtrack south

                }

            }

         }

     }

 }

 else

     success = false

 return success

 

 

 The goWest function will examine all the paths that start at the square to the west of the present square as

follows. If the square directly to the west is clear, is inside the maze, and has not been visited before, move into

this square and mark it as part of the path. (Note that you are moving from the east.) Check whether you are at the

exit. If you are, you’re done. Otherwise, try to find a path to the exit from here by trying all paths leaving this

square except the one going east (this would put you back in the square from which you just came) as follows.

Call goNorth ; if it is not successful, call goWest ; and if it is not successful, call goSouth . If goSouth is not suc-

cessful, mark this square as visited, move back into the square to the east, and return. The functions goEast and

 goSouth are analogous to goWest and goNorth .

 

 The input data to represent a maze is simple. For example, the previously given maze is represented by the

following lines of input, which can be in a text file:

 

 20     7 ← width and height of the maze in squares

 0  18 ← row and column coordinate of maze exit

 6  12 ← row and column coordinate of maze entrance

 xxxxxxxxxxxxxxxxxx  x

 x           x                 xxxx  x

 x xxxxx    xxxxx     xx  x

 x xxxxx   xxxxxxx xx  x

 x x                          xx xx x

 x xxxxxxxxxx  xx        x

 xxxxxxxxxxxx  xxxxxxx

 

 After the first three lines of numeric data in the file, each of the next lines corresponds to a row in the maze, and

each character in a line corresponds to a column in the maze. An x indicates a blocked square (part of the wall),

and a blank indicates a clear square. This notation is convenient, because you can see what the maze looks like as

you design it.

 

C++

Answers

(5)
Status NEW Posted 20 Dec 2017 11:12 AM My Price 8.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)