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

| backtracking
| Backtracking algorithms.
| 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/".

>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

{
test(); // run the tests
return 0;
}
test
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

  size | number of matrix size

Parameters

  currposrow | current position in rows
  currposcol | current position in columns
  maze | matrix where numbers are saved
  soln | matrix to problem solution

Returns
  0 on end

{
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;
}
}
std::cout
std::endl
T endl(T... args)
backtracking::rat_maze::solveMaze
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.

{
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 rows
int currposcol = 0; // Current position in columns
assert(backtracking::rat_maze::solveMaze<size>(currposrow, currposcol, maze,
soln) == 1);
}
std::array