Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
rat_maze.cpp File Reference

Implements <a href="https://www.codesdope.com/blog/article/backtracking-to- solve-a-rat-in-a-maze-c-java-pytho/". More...

#include <array>
#include <iostream>
#include <cassert>
Include dependency graph for rat_maze.cpp:

Namespaces

namespace  backtracking
 Backtracking algorithms.
 
namespace  rat_maze
 Functions for <a href="https://www.codesdope.com/blog/article/backtracking-to- solve-a-rat-in-a-maze-c-java-pytho/".
 

Functions

template<size_t size>
bool backtracking::rat_maze::solveMaze (int currposrow, int currposcol, const std::array< std::array< int, size >, size > &maze, std::array< std::array< int, size >, size > soln)
 Solve rat maze problem. More...
 
static void test ()
 Test implementations. More...
 
int main ()
 Main function. More...
 

Detailed Description

Implements <a href="https://www.codesdope.com/blog/article/backtracking-to- solve-a-rat-in-a-maze-c-java-pytho/".

target="_blank" >Rat in a Maze algorithm

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
David Leal

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
110 {
111 test(); // run the tests
112 return 0;
113}
static void test()
Test implementations.
Definition: rat_maze.cpp:84
Here is the call graph for this function:

◆ solveMaze()

template<size_t size>
bool backtracking::rat_maze::solveMaze ( int  currposrow,
int  currposcol,
const std::array< std::array< int, size >, size > &  maze,
std::array< std::array< int, size >, size >  soln 
)

Solve rat maze problem.

Template Parameters
sizenumber of matrix size
Parameters
currposrowcurrent position in rows
currposcolcurrent position in columns
mazematrix where numbers are saved
solnmatrix to problem solution
Returns
0 on end
47 {
48 if ((currposrow == size - 1) && (currposcol == size - 1)) {
49 soln[currposrow][currposcol] = 1;
50 for (int i = 0; i < size; ++i) {
51 for (int j = 0; j < size; ++j) {
52 std::cout << soln[i][j] << " ";
53 }
55 }
56 return true;
57 } else {
58 soln[currposrow][currposcol] = 1;
59
60 // if there exist a solution by moving one step ahead in a column
61 if ((currposcol < size - 1) && maze[currposrow][currposcol + 1] == 1 &&
62 solveMaze(currposrow, currposcol + 1, maze, soln)) {
63 return true;
64 }
65
66 // if there exists a solution by moving one step ahead in a row
67 if ((currposrow < size - 1) && maze[currposrow + 1][currposcol] == 1 &&
68 solveMaze(currposrow + 1, currposcol, maze, soln)) {
69 return true;
70 }
71
72 // the backtracking part
73 soln[currposrow][currposcol] = 0;
74 return false;
75 }
76}
T endl(T... args)
bool solveMaze(int currposrow, int currposcol, const std::array< std::array< int, size >, size > &maze, std::array< std::array< int, size >, size > soln)
Solve rat maze problem.
Definition: rat_maze.cpp:45
Here is the call graph for this function:

◆ test()

static void test ( )
static

Test implementations.

Returns
void
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/numerical_methods/rungekutta.cpp, and /Users/runner/work/C-Plus-Plus/C-Plus-Plus/sorting/wiggle_sort.cpp.
84 {
85 const int size = 4;
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}};
89
91
92 // Backtracking: setup matrix solution to zero
93 for (int i = 0; i < size; ++i) {
94 for (int j = 0; j < size; ++j) {
95 soln[i][j] = 0;
96 }
97 }
98
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}