-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
rat_maze.cpp
115 lines (104 loc) · 3.65 KB
/
rat_maze.cpp
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
/**
* @file
* @brief Implements [Rat in a
* Maze](https://www.codesdope.com/blog/article/backtracking-to-
* solve-a-rat-in-a-maze-c-java-pytho/) algorithm
*
* @details
* A Maze is given as N*N binary matrix of blocks where source block is the
* upper left most block i.e., maze[0][0] and destination block is lower
* rightmost block i.e., maze[N-1][N-1]. A rat starts from source and has to
* reach destination. The rat can move only in two directions: forward and down.
* In the maze matrix, 0 means the block is dead end and 1 means the block can
* be used in the path from source to destination.
*
* @author [Vaibhav Thakkar](https://github.com/vaithak)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <array> /// for std::array
#include <cassert> /// for assert
#include <iostream> /// for IO operations
/**
* @namespace backtracking
* @brief Backtracking algorithms
*/
namespace backtracking {
/**
* @namespace rat_maze
* @brief Functions for [Rat in a
* Maze](https://www.codesdope.com/blog/article/backtracking-to-
* solve-a-rat-in-a-maze-c-java-pytho/) algorithm
*/
namespace rat_maze {
/**
* @brief Solve rat maze problem
* @tparam size number of matrix size
* @param currposrow current position in rows
* @param currposcol current position in columns
* @param maze matrix where numbers are saved
* @param soln matrix to problem solution
* @returns `true` if there exists a solution to move one step ahead in a column
* or in a row
* @returns `false` for the backtracking part
*/
template <size_t size>
bool solveMaze(int currposrow, int currposcol,
const std::array<std::array<int, size>, size> &maze,
std::array<std::array<int, size>, size> soln) {
if ((currposrow == size - 1) && (currposcol == size - 1)) {
soln[currposrow][currposcol] = 1;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
std::cout << soln[i][j] << " ";
}
std::cout << std::endl;
}
return true;
} else {
soln[currposrow][currposcol] = 1;
// if there exist a solution by moving one step ahead in a column
if ((currposcol < size - 1) && maze[currposrow][currposcol + 1] == 1 &&
solveMaze(currposrow, currposcol + 1, maze, soln)) {
return true;
}
// if there exists a solution by moving one step ahead in a row
if ((currposrow < size - 1) && maze[currposrow + 1][currposcol] == 1 &&
solveMaze(currposrow + 1, currposcol, maze, soln)) {
return true;
}
// the backtracking part
soln[currposrow][currposcol] = 0;
return false;
}
}
} // namespace rat_maze
} // namespace backtracking
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
const int size = 4;
std::array<std::array<int, size>, size> maze = {
std::array<int, size>{1, 0, 1, 0}, std::array<int, size>{1, 0, 1, 1},
std::array<int, size>{1, 0, 0, 1}, std::array<int, size>{1, 1, 1, 1}};
std::array<std::array<int, size>, size> soln{};
// Backtracking: setup matrix solution to zero
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
soln[i][j] = 0;
}
}
int currposrow = 0; // Current position in the rows
int currposcol = 0; // Current position in the columns
assert(backtracking::rat_maze::solveMaze<size>(currposrow, currposcol, maze,
soln) == 1);
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}