Write a program to read a maze from a text file into your array, print the unsolved maze, solve the maze, and then print the solution. You may assume the maze will fit in a 24-row by 81-column character array (for 80 character C-strings). The maze will be in a file with the number of rows and columns on the first line, followed by the lines defining the maze, with '' *representing a wall and ' ' (space) representing a corridor. For example, here is a small (8-by-12) maze (maze8-12.txt):

8 12
  **********
* *        *
*   ****** *
*****      *
*     ******
*** ***    *
*       ** *
**********  

You may assume that the maze will always have a solution. The program is easily generalized to show if there is no solution, but this is not required for this assignment. For an n-by-m maze held in a char array a[NRows][NCols], Nrows >= n and NCols >= m, the starting point (at the top left) is a[0][0], and the ending point (at the bottom right) is at a[n-1][m-1]. When you print the solved maze (print to a file from within the program), show the path through the maze by marking all locations in the solution path with '#'. For example, here is the same maze printed showing the solution:

##**********
*#*########*
*###******#*
*****######*
*  ###******
***#***####*
*  #####**#*
**********##

Notice, moves down "blind alleys" are left off the solution. Use the recursive backtracking algorithm for this program.

Here is my solution:

#include<iostream>
#include<fstream>
using namespace std;

const int MAXROW = 24;
const int MAXCOL = 81;

void readMaze()
{
    ifstream in;
    char a[MAXROW][MAXCOL];
    int rows, columns;

    in >> rows >> columns;
    cout << rows << " " << columns << endl;
    in >> a[rows][columns];

    for(rows = 0; rows < MAXROW; rows++)
    {
        for(columns = 0; columns < MAXCOL; columns++)
        {
            cout << a[rows][columns];
        }
        cout << endl;
    }
}

void solveMaze(int rows, int columns)
{
    char a[MAXROW][MAXCOL];

    if((rows > 0 && rows <= MAXROW) && (columns > 0 && columns <= MAXCOL))
    {
        if(a[rows][columns] == '#')
        {
            return;
        }
        if(a[rows][columns] == ' ')
        {
            a[rows][columns] = '*';
            solveMaze(rows, columns+1);
            solveMaze(rows, columns-1);
            solveMaze(rows-1, columns);
            solveMaze(rows+1, columns);
        }
    }
}

int main()
{
    char inFile[9] = "maze.txt";
    ifstream in;
    int rows, columns;

    cout << "Enter file name: ";
    cin >> inFile;

    in.open(inFile);

    /*if(!inFile)
    {
        cout << "Error" << endl;
        return 1;
    }*/

    cout << "Maze before solve: " << endl;
    readMaze();
    cout << "Maze after: " << endl;
    solveMaze(rows,columns);
    readMaze();
    in.close();
    return 0;
}

My problem is: When I am trying to print the output, all I am getting is garbage output. Is there anyway I can fix this?

One thing I noticed is that you check for row > 0 and column > 0, but in C++ the first item in an array would be at position 0, and the last item would be at, for example MAXROW-1. So you are probably going beyond the ends of your arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.