|
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 | + */ |
11 | 18 |
|
12 |
| -using namespace std; |
| 19 | +#include <array> |
| 20 | +#include <iostream> |
| 21 | +#include <cassert> |
13 | 22 |
|
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) { |
16 | 48 | if ((currposrow == size - 1) && (currposcol == size - 1)) {
|
17 | 49 | soln[currposrow][currposcol] = 1;
|
18 | 50 | for (int i = 0; i < size; ++i) {
|
19 | 51 | for (int j = 0; j < size; ++j) {
|
20 |
| - cout << soln[i][j]; |
| 52 | + std::cout << soln[i][j] << " "; |
21 | 53 | }
|
22 |
| - cout << endl; |
| 54 | + std::cout << std::endl; |
23 | 55 | }
|
24 |
| - return 1; |
| 56 | + return true; |
25 | 57 | } else {
|
26 | 58 | soln[currposrow][currposcol] = 1;
|
27 | 59 |
|
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 |
29 | 61 | if ((currposcol < size - 1) && maze[currposrow][currposcol + 1] == 1 &&
|
30 | 62 | solveMaze(currposrow, currposcol + 1, maze, soln)) {
|
31 |
| - return 1; |
| 63 | + return true; |
32 | 64 | }
|
33 | 65 |
|
34 | 66 | // if there exists a solution by moving one step ahead in a row
|
35 | 67 | if ((currposrow < size - 1) && maze[currposrow + 1][currposcol] == 1 &&
|
36 | 68 | solveMaze(currposrow + 1, currposcol, maze, soln)) {
|
37 |
| - return 1; |
| 69 | + return true; |
38 | 70 | }
|
39 | 71 |
|
40 | 72 | // the backtracking part
|
41 | 73 | soln[currposrow][currposcol] = 0;
|
42 |
| - return 0; |
| 74 | + return false; |
43 | 75 | }
|
44 | 76 | }
|
| 77 | +} // namespace rat_maze |
| 78 | +} // namespace backtracking |
45 | 79 |
|
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}}; |
49 | 89 |
|
50 |
| - int soln[size][size]; |
| 90 | + std::array<std::array<int, size>, size> soln{}; |
51 | 91 |
|
| 92 | + // Backtracking: setup matrix solution to zero |
52 | 93 | for (int i = 0; i < size; ++i) {
|
53 | 94 | for (int j = 0; j < size; ++j) {
|
54 | 95 | soln[i][j] = 0;
|
55 | 96 | }
|
56 | 97 | }
|
57 | 98 |
|
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 |
61 | 112 | return 0;
|
62 | 113 | }
|
0 commit comments