C ++ job 01

xiaoxiao2021-03-06  103

Program # 2 - MAZE TRAVERSAL

Maze Traversal:

You will be given two files that contain ASCII mazes, and your program should first read in the maze into arrays (or vectors) and then recursively find a path through the maze. When the program has found a path through the maze it should print to the screen the path that it found. your path can only be up, down, left or right (sorry no diagonal moves). For your final path you are not allowed to step on a square more than once (so treat previously marked squares as impassable.

Your program should prompt the user for the file name that contains the maze, and then proceed to read in the file. The format of the file is that the first line will contain information about the numbers of rows and the number of columns for the maze .

Key to The Maze Characters:

% Wall

EMPTY FLOOR

e entrance (Starting position)

x exit (ending position)

Step That Your Program Takes .... Mark Your Trail as you go .... and don't forget to ERASE

Your Marks if you need to go back

Here is what maze2.txt looks like:

15 x 28 %%%%%%%%%%%%%%%%%%%%% ...% .....% .......% ....... %% ...% ....% ....... %%% .... %% .. %% .. %% .... ..% ...% .... E %%%%%. %%%. %%% .....% ... %% ... %% ...... %% ....% ...% .......%. %% .. %%%% .....% ....% .... %%% .. %% ..... %% .... %% ... %%%% .... %%%%. %%%%%%% ....%.%.% .... .... %% ...............% ........ %%%%%% .......% ...% ..% .. %%%%%%% .....%.% ... %%% ....% ....... %%%% ....% ... .% .... %%% .... %%. %%% ...%.%.% ...% ..... %%% ... %%%%% X %%%%%%%%%%%%%%%%%%%%%%%

Hints:

As you read in the maze you may want to save the location of the starting position. Think About The Solution First and Write Some Pseudo-Code Before Trying to Write The Recursive Part of The Solution.

what is your ending condition what do you want to try each time you call the function what happens in your function each time it is successful, or each time it is unsuccessful One way to solve it is to have your function declaration something like this?: bool takeStep (int row, int col); where the function returns true if it finds the end (through a chain of calls) or false if this path is a dead-end Remember where a function call will return when it is done (. successfull or not) ..... back to it's calling point.Debugging Suggestions:. For debugging feel free to make a smaller maze (5x5) to see if your algorithm works, or even a maze with no internal walls Also it is always A good idea to print Out The Maze After Every Step or Two So You Can See The Progress of You Program (you can read a char Into A Junk Variable To Cause A Pause In Your Program).

Turn in: A print out of your code, and the two mazes with your path The two mazes you can capture from the command window (black screen) by using Select All and then Copy from the command windows menus, then you can paste your. Output Below Your Programs Code as a comment.

转载请注明原文地址:https://www.9cbs.com/read-100981.html

New Post(0)