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 updated solution:

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

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

char a[MAXROW][MAXCOL];

void readMaze(ifstream &in)
{
    int rows, columns;

    in >> rows >> columns;
    cout << rows << " " << columns << endl;

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

void solveMaze(int rows, int columns)
{
    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[20] = "maze.txt";
    ifstream in;
    int rows, columns;

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

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

    in.open(inFile);

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

Here is my output:

Enter file name: maze.t
Maze before solve:
0 2147348480

Maze after:
0 2147348480

My output is still not coming out correct. What else do I need to fix in order to print the unsolved maze and solution?

Recommended Answers

All 4 Replies

use bool for solving the maze recursively.

Here is a good tutorial for backtracking algorithm :

Click Here

Hope that helps :)

I replied to this thread yesteday explaining a big error in the code, but my reply has gone. Daniweb?

This post was made an hour ago.

This is the other post that you are talking about: Click Here

Ah, ok, almost but not quite identical!

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.