Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this: Part 2: Recursively searching a Maze. Your next task is t

ID: 3640419 • Letter: I

Question

I need help with this:

Part 2: Recursively searching a Maze. Your next task is to write a program that simulates the movements of an untrained mouse searching for a path through a maze from a starting point S to an ending point E. To do this, you must define a recursive function that calls itself to move N,S,E,W through open space starting at S and ending at E. For example, if the mouse is at location (row,col) and there is a wall at (row+1,col) and the mouse has already visited (row-1,col) then you must recursively search (row,col+1) and (row,col-1) to see if they eventually lead to the ending point E.

If you manually search the maze above, you should see that the there are times when the mouse will reach a dead end and they must "backtrack" to find the correct path. In order for your program to support this you must store and update information about which (row,col) Maze positions have been visited by the mouse and which positions are part of the final path from S to E.

Finally, when your program solves the maze, you need to output/display this information in some way. One option would be to print a sequence of (row,col) coordinates taken by the mouse. Another option would be to display the correct path on the maze itself with another character. This output/display may also be of help when debugging your recursive maze search function.


AND HERE IS WHAT I HAVE(IT COMPILES PERFECTLY):
Maze.h
#include
#include
#include
using namespace std;

//-----------------------------------------------------------
// Define the Maze class interface
//-----------------------------------------------------------
class Maze
{
public:
// Constructors
Maze();
~Maze();

// Methods
void ReadMaze(string name);
void WriteMaze(string name);
void PrintMaze();
void GetMaze(int r, int c, char &value);
void SetMaze(int r, int c, char value);
void GetStart(int &r, int &c);
void SetStart(int r, int c);
void GetEnd(int &r, int &c);
void SetEnd(int r, int c);
private:
int num_rows, num_cols;
int start_row, start_col;
int end_row, end_col;
char maze[100][100];
};

Maze.cpp
#include "Maze.h"
#include
#include
using namespace std;
//-----------------------------------------------------------
// Constructor function
//-----------------------------------------------------------
Maze::Maze()
{
num_rows = 0;
num_cols = 0;
start_row = 0;
start_col = 0;
end_row = 0;
end_col = 0;
}

//-----------------------------------------------------------
// Destructor function
//-----------------------------------------------------------
Maze::~Maze()
{
}

//-----------------------------------------------------------
// Read maze from ascii file
//-----------------------------------------------------------
void Maze::ReadMaze(string name)
{
// Read maze.txt header
ifstream din;
din.open(name.c_str());
din >> num_rows >> num_cols;
din >> start_row >> start_col;
din >> end_row >> end_col;

// Read maze.txt body
string line;
getline(din, line);
for (int r=0; r {
getline(din, line);
for (int c=0; c if (c < (int)line.length())
maze[r][c] = line[c];
}
din.close();
}

//-----------------------------------------------------------
// Write maze to ascii file
//-----------------------------------------------------------
void Maze::WriteMaze(string name)
{
// Write maze.txt header
ofstream dout;
dout.open(name.c_str());
dout << num_rows << " " << num_cols << endl;
dout << start_row << " " << start_col << endl;
dout << end_row << " " << end_col << endl;

// Write maze.txt body
for (int r=0; r {
for (int c=0; c dout << maze[r][c];
dout << endl;
}
dout.close();
}

//-----------------------------------------------------------
// Print maze to screen
//-----------------------------------------------------------
void Maze::PrintMaze()
{
// Print maze
for (int r=0; r {
for (int c=0; c cout << maze[r][c];
cout << endl;
}
}

//-----------------------------------------------------------
// Get start location
//-----------------------------------------------------------
void Maze::GetStart(int &r, int &c)
{
r = start_row;
c = start_col;
}

//-----------------------------------------------------------
// Set start location
//-----------------------------------------------------------
void Maze::SetStart(int r, int c)
{
start_row = r;
start_col = c;
}

//-----------------------------------------------------------
// Get end location
//-----------------------------------------------------------
void Maze::GetEnd(int &r, int &c)
{
r = end_row;
c = end_col;
}

//-----------------------------------------------------------
// Set end location
//-----------------------------------------------------------
void Maze::SetEnd(int r, int c)
{
end_row = r;
end_col = c;
}

main.cpp
#include "Maze.h"
int main()
{
Maze m;


m.ReadMaze("maze.txt");
m.PrintMaze();
m.WriteMaze("maze.out");
return 0;
}

Explanation / Answer

#include #include #include #include #include "maze.h" #include "grid.h" #include "cmumovie.h" float RUBIN_CONST=1.1; extern int global_movie_expand; extern vector net_paths_segment; extern vector vi_violations; long long cells_expanded=0; int nets_routed=0; int dir_cost=10; int via_cost=10; int bend_cost=1; int global_num_cells_frame=10; int dir_cost_array[4][6]; list maze_route(grid &g, list &source, int net_dest, list &remaining); bool operatorac2.total_cost); } static int estimated_pathcost(cell c, cube r) { int dx=0,dy=0,dz=0; if (c.x r.x2) { dx=c.x - r.x2; } if (c.y r.y2) { dy=c.y-r.y2; } if (c.l r.z2) { dz=c.l-r.z2; } return dx+dy+dz*(via_cost); } // estimated path cost for a destination pin // to a source cube -- how to deal with levels static int estimated_pathcost(pin p, cube r) { int dx=0,dy=0,dz=0; if (p.x r.x2) { dx=p.x - r.x2; } if (p.y r.y2) { dy=p.y-r.y2; } // pins have no level information /* if (p.l r.z2) { dz=p.l-r.z2; } */ // return dx+dy+dz*(via_cost+1); return dx+dy; } static cube get_bounding_cube(list &vp) { int i,count=0; int vp_size=vp.size(); cube r; r.x1=vp.front().x; r.x2=vp.front().x; r.y1=vp.front().y-1; r.y2=vp.front().y+1; list::iterator lpi; lpi=vp.begin(); ++lpi; for (;lpi!=vp.end();++lpi,++count) { if (lpi->xx; if ((lpi->x)>r.x2) r.x2=(lpi->x); if ((lpi->y-1)y-1); if ((lpi->y+2)>r.y2) r.y2=(lpi->y+1); } r.z1=0; r.z2=1; return r; } // cells from pin cannot be in lc to begin with static void add_cells_from_pin(list &lc, pin p) { int y; int l=-1,h=1; if (p.pad) { l=0;h=0; } cell c; for (y=l;yz1==lwsi->z2) { // not a via if (lwsi->x1 != lwsi->x2) { // x dir c.y=lwsi->y1; c.l=lwsi->z1; for (c.x=min(lwsi->x1,lwsi->x2); c.xx1,lwsi->x2));c.x++) ret.push_back(c); } else { // y dir c.x=lwsi->x1; c.l=lwsi->z1; for (c.y=min(lwsi->y1,lwsi->y2); c.yy1,lwsi->y2));c.y++) ret.push_back(c); } } } } list path_to_segment(list &path) { list ret; wire_segment cur_seg,tseg; int dir=-1; cur_seg.x1=-1; cur_seg.x2=-1; list::iterator lci; for (lci=path.begin();lci!=path.end();++lci) { if (lci->l2>=0) { // we have a via if (cur_seg.x2>=0) { // current segment is valid, so push it onto return list ret.push_back(cur_seg); } cur_seg.x1=lci->x; cur_seg.y1=lci->y; cur_seg.z1=lci->l; cur_seg.x2=lci->x; cur_seg.y2=lci->y; cur_seg.z2=lci->l2; ret.push_back(cur_seg); cur_seg.x1=-1; cur_seg.x2=-1; } else { if (cur_seg.x1==-1) { //we have no current segment, //new one will be just this cell cur_seg.x1=lci->x; cur_seg.y1=lci->y; cur_seg.z1=lci->l; cur_seg.x2=lci->x; cur_seg.y2=lci->y; cur_seg.z2=lci->l; dir=-1; } else { tseg.x1=cur_seg.x2; tseg.y1=cur_seg.y2; tseg.z1=cur_seg.z2; tseg.x2=lci->x; tseg.y2=lci->y; tseg.z2=lci->l; if (get_seg_dir(tseg)==-2) { //cells are not adjacent ret.push_back(cur_seg); cur_seg.x1=lci->x; cur_seg.y1=lci->y; cur_seg.z1=lci->l; cur_seg.x2=lci->x; cur_seg.y2=lci->y; cur_seg.z2=lci->l; dir=-1; } else { if (dir==-1 || dir==get_seg_dir(tseg)) { cur_seg.x2=lci->x; cur_seg.y2=lci->y; cur_seg.z2=lci->l; dir=get_seg_dir(tseg); } else { ret.push_back(cur_seg); cur_seg=tseg; dir=get_seg_dir(tseg); } } } } } if (cur_seg.x1!=-1) { ret.push_back(cur_seg); } return ret; } list find_pin_to_route(list &remaining_pins, list &used_pins) { list::iterator lpi,best; int pin_to_route_cost=1