Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
rat_maze.cpp File Reference

Implements Rat in a Maze algorithm. More...

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

Namespaces

namespace  backtracking
 for vector container
 
namespace  rat_maze
 Functions for Rat in a Maze algorithm.
 

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.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implements 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
112 {
113 test(); // run self-test implementations
114 return 0;
115}
static void test()
Self-test implementations.
Definition rat_maze.cpp:86
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
true if there exists a solution to move one step ahead in a column or in a row
false for the backtracking part
49 {
50 if ((currposrow == size - 1) && (currposcol == size - 1)) {
51 soln[currposrow][currposcol] = 1;
52 for (int i = 0; i < size; ++i) {
53 for (int j = 0; j < size; ++j) {
54 std::cout << soln[i][j] << " ";
55 }
57 }
58 return true;
59 } else {
60 soln[currposrow][currposcol] = 1;
61
62 // if there exist a solution by moving one step ahead in a column
63 if ((currposcol < size - 1) && maze[currposrow][currposcol + 1] == 1 &&
64 solveMaze(currposrow, currposcol + 1, maze, soln)) {
65 return true;
66 }
67
68 // if there exists a solution by moving one step ahead in a row
69 if ((currposrow < size - 1) && maze[currposrow + 1][currposcol] == 1 &&
70 solveMaze(currposrow + 1, currposcol, maze, soln)) {
71 return true;
72 }
73
74 // the backtracking part
75 soln[currposrow][currposcol] = 0;
76 return false;
77 }
78}
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:47
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
86 {
87 const int size = 4;
89 std::array<int, size>{1, 0, 1, 0}, std::array<int, size>{1, 0, 1, 1},
90 std::array<int, size>{1, 0, 0, 1}, std::array<int, size>{1, 1, 1, 1}};
91
93
94 // Backtracking: setup matrix solution to zero
95 for (int i = 0; i < size; ++i) {
96 for (int j = 0; j < size; ++j) {
97 soln[i][j] = 0;
98 }
99 }
100
101 int currposrow = 0; // Current position in the rows
102 int currposcol = 0; // Current position in the columns
103
104 assert(backtracking::rat_maze::solveMaze<size>(currposrow, currposcol, maze,
105 soln) == 1);
106}