Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objctivs: 1) Us rcursion; 2) Undrstand and apply backtracking; 3) Us STL containr; Projct dscription: Writ a C++ program that, givn a starting point, finds

Objctivs: 1) Us rcursion; 2) Undrstand and apply backtracking; 3) Us STL containr;

Projct dscription: Writ a C++ program that, givn a starting point, finds its way out of a maz. Th mazs map will b rad from a fil at th start of th program. Your cod must work for all lgal mazs.. Th maz is a rctangular grid rprsntd as a 2D array, and th xit (if thr is on) should b placd on an outr row or column of th play ara. Th program should run until th xit to th maz is found or until it is dtrmind that thr is no xit (aftr xploring all travrsabl clls). xploration of th maz is don by rcursivly invoking a function and marking th clls visitd with a spcial charactr (an lctronic brad crumb to kp from rvisiting xplord clls). Th lgal movs ar to clls adjacnt but not diagonal to th cll currntly occupid. Candidats for th nxt mov ar clls that ar travrsabl and hav not bn prviously visitd. If th spcially markd xit cll is ncountrd th gam should xit with a mssag that th xit was found. Othrwis, aftr xploring th whol maz, a mssag is output stating that thr is no xit.

image text in transcribed

At lft is an instanc of a maz. Not th attributs of a lgal maz:

X marks non-travrsabl clls Th clls in rd ar not part of th maz map rad from th fil. Thy mark th boundary of th maz, and must b rprsntd as not travrsabl. Th actual play ara has 2 fwr columns and 2 fwr rows than th 10x10 maz map array (i.. you will rad a maximum 8x8 play ara from th fil). Blu squars ar walls.

* (yllow) shows travrsabl clls that hav bn prviously visitd and should not b rvisitd whn locating th xit.

O clls (grn) ar travrsabl clls that hav not bn visitd. All travrsabl clls must b rachabl from any othr travrsabl cll

marks th xit (outlind gray cll) If it xists, It must b placd on on of th outr rows or columns of th play ara. Th xit must b rachabl from any travrsabl clls in th maz (it cant b containd within walls).

Rquirmnts:

1. Your program must b split into 3 fils. Thr will b a class (with sparat intrfac and implmntation fils), and a drivr fil. Th rquirmnts for ths ar spcifid blow:

Th Maz class This class rprsnts a maz

Fils must b namd maz.h and maz.cpp

Class must b namd Maz

Th intrfac (hadr fil) is providd.

You should implmnt th intrfac fil in a .cpp implmntation fil

All data mmbrs must b of th typ spcifid in th hadr fil

All mmbr functions in th intrfac fil must b implmntd as dclard Howvr you hav flxibility in how you choos to implmnt ach

Th constructor will accpt a fil argumnt and will opn and rad th fil and construct th maz. Th fil contains a rctangular maz no largr than 8 clls by 8 clls (your cod must b abl to handl smallr mazs having a minimum of two rows or two columns). Th first lin in th fil has th dimnsions of th maz (# of rows followd by # of columns sparatd by a spac). An xampl maz fil (5 rows by 4 columns) is shown blow. Not: Thr ar no spacs btwn cll lgnd symbols in th input fil.

5 4

OOXO

XOOO

OOX

XOOX

XOOX

Th Print() function should output th mazs currnt stat to th scrn (using th lgnd dscribd abov and including clls visitd thus far). S sampl output.

Function GtStartPt() rturns a random starting point from th travrsabl clls in th maz. Th coords data mmbr should b usd to slct this point.

Th Findxit() function is a rcursiv function that xplors th maz. It has two int paramtrs indicating th playrs currnt row and column (in that ordr), and a bool rfrnc variabl that rprsnts whthr or not th xit has bn found. You should considr th 4 qustions to ask whn planning to us rcursiv algorithms, as you dsign your approach:

How ar smallr instancs of th problm dfind?

How dos ach rcursiv call mak th problm instanc smallr?

What condition(s) can srv as th bas cas(s)?

Will th smallr instancs of th problm rach th bas cas(s)?

A drivr, or clint, fil

Must b namd proj5.cpp

Must contain th lin srand(1000); as th first lin of th main function

Must dclar th fil objct containing th maz map. This fil must b namd maz.txt, and must b in th sam foldr as th sourc fils.

Must instantiat maz, obtain th starting point, and invok th Findxit function.

Th output from th Findxit function should b usd to dtrmin if th xit was found, or if th maz containd no xit. Th appropriat mssag should b output bfor xiting th program.

2. Sampl output:

a) This output show program xcution for th sampl grid prsntd abov, with th playr starting at cll 2,3 (Not: th ordr of sarching adjacnt clls impacts th path takn to th xit).

image text in transcribed

//Maze.h

#ifndef MAZE_H #define MAZE_H #include  #include  struct Coordinate // 2D point consisting of x and y coordinates { double x, y; Coordinate(double px, double py) : x(px), y(py) {} // Constructor with initializer list }; class Maze // Represents Maze class' data and function members { public: Maze(std::ifstream&); // Constructor: takes file object and reads maze map from file void Print(); // Displays the maze and its state Coordinate GetStartPt(); // Returns a randomly chosen maze starting point - use coords member void FindExit(int, int, bool&); // Recursive function that attempts to find the exit private: char maze[10][10]; // 2D array that holds maze - outer columns and rows not traversable int maxRows; // Maximum number of rows - excludes outer walls int maxCols; // Maximum number of columns - excludes outer walls std::vector coords; // holds initially traversable locations on the map }; #endif 

00 o o o o o 00 o o o o o

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions