Recursive maze solving

Hi, I finished my code but the output result does not look right. I can not find my problem, Can anyone help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  #include <fstream>
#include <iostream>


#include <string>

using namespace std;

enum {
	space = ' ', path = '@'
};






void printmaze(char**maze, int row, int col)
{
	// Display the result
	for (int i = 0; i <row; i++)
	{
		for (int j = 0; j < col; j++)

			cout << maze[i][j] << " ";

		cout << endl;

	}

}

bool done = false;

void find_paths(char**m, int rows, int cols, int r, int c)
{
	if (done) {
		return;
	}
	if (r == rows - 1)
	{
		printmaze(m, rows, cols);
		done = true;
	}

	else {
		if (r > 0 && m[r - 1][c] == space) // UP
		{
			m[r - 1][c] = path;
			find_paths(m, r - 1, c, rows, cols);
			m[r - 1][c] = space;
		}
		if (m[r + 1][c] == space) // DOWN
		{
			m[r + 1][c] = path;
			find_paths(m, r + 1, c, rows, cols);
			m[r + 1][c] = space;
		}
		if (c > 0 && m[r][c - 1] == space) // LEFT
		{
			m[r][c - 1] = path;
			find_paths(m, r, c - 1, rows, cols);
			m[r][c - 1] = space;
		}
		if (c < cols && m[r][c + 1] == space) // RIGHT
		{
			m[r][c + 1] = path;
			find_paths(m, r, c + 1, rows, cols);
			m[r][c + 1] = space;
		}
	}
}

int main()
{
	// Variable Declaration

	int row, col;

	// Holds the maze structure from input file
	ifstream inputfile;                   // Variable to read the input file

	// Open the input file and exit if file not found
	inputfile.open("maze.dat");
	if (inputfile.eof())
	{
		cout << "File not found." << endl;
		system("pause");
		return 0;
	}

	// Read and display the size of maze
	inputfile >> row >> col;
	cout << row << " " << col << endl;


	// Read the maze structure from input file and store in maze[][] array
	string line;
	char** maze;
	maze = new char*[row + 1];
	for (int i = 0; i < row; i++)
	{
		maze[i] = new char[col + 1];

	}
	int i = 0;
	getline(inputfile, line);
	while (getline(inputfile, line) && i <row)
	{
		for (int j = 0; j < col; j++)
		{
			maze[i][j] = line[j];
		}
		i++;

	}
	printmaze(maze, row, col);

	find_paths(maze, row, col,0, 1);



	// Close the file
	inputfile.close();

	system("pause");
	return 0;
}


20 20
+   + + + + + + + + + + + + + + + + + +
+                     +     + +   + + +
+ + +   + + + + + +       + +       + +
+   + + +         + + +         + + + +
+       +   + +   + + + + + +         +
+ + +       + +           + +   + +   +
+       + + +   + + + +   + +   + +   +
+   + + + +     + +       + + + + +   +
+         +   + + +   + + +           +
+   + +   +   + + +   + + + + + +   + +
+     + + + + +       + + +         + +
+   + + +         + + + +     + +   + +
+       + + + +             + + +     +
+ + +         + + + + + + + + + + +   +
+ +     + + + + + + +                 +
+ +   + + +         + + + + + + + +   +
+ +   + + + + + +                 +   +
+       + + +       + + + + + +   + + +
+ + + +           + + +           + + +
+ + + + + + + + + + + + +   + + + + + +
Press any key to continue . . .
the output result does not look right

What do you mean "does not look right"?

I notice that you are passing the parameters in the wrong order in your recursive calls.
And c < cols should be c < cols - 1.
And you're missing r < rows - 1 to protect the r + 1 access.
There may be other problems.
Last edited on
The order of arguments in the function calls on lines 50, 56, 62, 68 does not reflect that in the function declaration.

How are we supposed to know what "the output does not look right" means?
Hi, sorry for not clear words. The program should output the path but it did not
Topic archived. No new replies allowed.