Skip to content

Commit 9565089

Browse files
authored
[fix/docs]: Improve backtracking/rat_maze.cpp (#1084)
* [fix/docs]: Improve backtracking/rat_maze.cpp * test: Added tests * test: Move tests to a separate test function
1 parent 06b6714 commit 9565089

File tree

1 file changed

+78
-27
lines changed

1 file changed

+78
-27
lines changed

backtracking/rat_maze.cpp

+78-27
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,113 @@
1-
/*
2-
A Maze is given as N*N binary matrix of blocks where source block is the
3-
upper left most block i.e., maze[0][0] and destination block is lower
4-
rightmost block i.e., maze[N-1][N-1]. A rat starts from source and has to
5-
reach destination. The rat can move only in two directions: forward and down.
6-
In the maze matrix, 0 means the block is dead end and 1 means the block can
7-
be used in the path from source to destination.
8-
*/
9-
#include <iostream>
10-
#define size 4
1+
/**
2+
* @file
3+
* @brief Implements [Rat in a
4+
* Maze](https://www.codesdope.com/blog/article/backtracking-to-
5+
* solve-a-rat-in-a-maze-c-java-pytho/) algorithm
6+
*
7+
* @details
8+
* A Maze is given as N*N binary matrix of blocks where source block is the
9+
* upper left most block i.e., maze[0][0] and destination block is lower
10+
* rightmost block i.e., maze[N-1][N-1]. A rat starts from source and has to
11+
* reach destination. The rat can move only in two directions: forward and down.
12+
* In the maze matrix, 0 means the block is dead end and 1 means the block can
13+
* be used in the path from source to destination.
14+
*
15+
* @author [Vaibhav Thakkar](https://github.com/vaithak)
16+
* @author [David Leal](https://github.com/Panquesito7)
17+
*/
1118

12-
using namespace std;
19+
#include <array>
20+
#include <iostream>
21+
#include <cassert>
1322

14-
int solveMaze(int currposrow, int currposcol, int maze[size][size],
15-
int soln[size][size]) {
23+
/**
24+
* @namespace backtracking
25+
* @brief Backtracking algorithms
26+
*/
27+
namespace backtracking {
28+
/**
29+
* @namespace rat_maze
30+
* @brief Functions for [Rat in a
31+
* Maze](https://www.codesdope.com/blog/article/backtracking-to-
32+
* solve-a-rat-in-a-maze-c-java-pytho/) algorithm
33+
*/
34+
namespace rat_maze {
35+
/**
36+
* @brief Solve rat maze problem
37+
* @tparam size number of matrix size
38+
* @param currposrow current position in rows
39+
* @param currposcol current position in columns
40+
* @param maze matrix where numbers are saved
41+
* @param soln matrix to problem solution
42+
* @returns 0 on end
43+
*/
44+
template <size_t size>
45+
bool solveMaze(int currposrow, int currposcol,
46+
const std::array<std::array<int, size>, size> &maze,
47+
std::array<std::array<int, size>, size> soln) {
1648
if ((currposrow == size - 1) && (currposcol == size - 1)) {
1749
soln[currposrow][currposcol] = 1;
1850
for (int i = 0; i < size; ++i) {
1951
for (int j = 0; j < size; ++j) {
20-
cout << soln[i][j];
52+
std::cout << soln[i][j] << " ";
2153
}
22-
cout << endl;
54+
std::cout << std::endl;
2355
}
24-
return 1;
56+
return true;
2557
} else {
2658
soln[currposrow][currposcol] = 1;
2759

28-
// if there exist a solution by moving one step ahead in a collumn
60+
// if there exist a solution by moving one step ahead in a column
2961
if ((currposcol < size - 1) && maze[currposrow][currposcol + 1] == 1 &&
3062
solveMaze(currposrow, currposcol + 1, maze, soln)) {
31-
return 1;
63+
return true;
3264
}
3365

3466
// if there exists a solution by moving one step ahead in a row
3567
if ((currposrow < size - 1) && maze[currposrow + 1][currposcol] == 1 &&
3668
solveMaze(currposrow + 1, currposcol, maze, soln)) {
37-
return 1;
69+
return true;
3870
}
3971

4072
// the backtracking part
4173
soln[currposrow][currposcol] = 0;
42-
return 0;
74+
return false;
4375
}
4476
}
77+
} // namespace rat_maze
78+
} // namespace backtracking
4579

46-
int main(int argc, char const *argv[]) {
47-
int maze[size][size] = {
48-
{1, 0, 1, 0}, {1, 0, 1, 1}, {1, 0, 0, 1}, {1, 1, 1, 1}};
80+
/**
81+
* @brief Test implementations
82+
* @returns void
83+
*/
84+
static void test(){
85+
const int size = 4;
86+
std::array<std::array<int, size>, size> maze = {
87+
std::array<int, size>{1, 0, 1, 0}, std::array<int, size>{1, 0, 1, 1},
88+
std::array<int, size>{1, 0, 0, 1}, std::array<int, size>{1, 1, 1, 1}};
4989

50-
int soln[size][size];
90+
std::array<std::array<int, size>, size> soln{};
5191

92+
// Backtracking: setup matrix solution to zero
5293
for (int i = 0; i < size; ++i) {
5394
for (int j = 0; j < size; ++j) {
5495
soln[i][j] = 0;
5596
}
5697
}
5798

58-
int currposrow = 0;
59-
int currposcol = 0;
60-
solveMaze(currposrow, currposcol, maze, soln);
99+
int currposrow = 0; // Current position in rows
100+
int currposcol = 0; // Current position in columns
101+
102+
assert(backtracking::rat_maze::solveMaze<size>(currposrow, currposcol, maze,
103+
soln) == 1);
104+
}
105+
106+
/**
107+
* @brief Main function
108+
* @returns 0 on exit
109+
*/
110+
int main() {
111+
test(); // run the tests
61112
return 0;
62113
}

0 commit comments

Comments
 (0)