0% found this document useful (0 votes)
90 views10 pages

Rat in a Maze: Backtracking Solution

Uploaded by

darkstaar7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views10 pages

Rat in a Maze: Backtracking Solution

Uploaded by

darkstaar7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

RAT IN A MAZE

BACKTRACKING PROBLEM
PROBLEM DEFINITON
• Maze Representation:
• The maze is represented as a 2D grid or
matrix.
• Each cell in the maze has a value:
⚬ 1: Open cell that the rat can move
through.
⚬ 0: Blocked cell that the rat cannot pass.
• Objective:
• Find a path from the starting cell (usually at
the top-left corner, (0, 0)) to the destination
cell (usually the bottom-right corner, (n-1, n-
1)).
• The path must consist of only open cells (1),
and the rat can move in specified directions
(commonly up, down, left, or right).
SOLUTION APPORACH:
The Rat in a Maze problem is typically solved
using backtracking, an algorithmic technique to
explore all potential solutions and backtrack
when encountering invalid paths.

WHY BACKTRACKING?
• Exhaustive Search: Backtracking ensures
that all possible paths are explored, making
it suitable for problems with multiple
solutions or unknown paths.
• Recursive Exploration: The maze can be
broken into sub-problems where the rat
moves step-by-step. Recursive backtracking
handles this naturally.
ALGORITHM EXPLANATION:

• Algorithm Explanation
1. Input Representation
• Represent the maze as a 2D grid of size n×nn \times nn×n.
• maze[i][j]=1maze[i][j] = 1maze[i][j]=1: The cell is open and can be
traversed.
• maze[i][j]=0maze[i][j] = 0maze[i][j]=0: The cell is blocked.
2. Output
• A boolean indicating whether a path exists.
• Optionally, a solution matrix where the path is marked (e.g., cells part of
the path are 1).
3. Movement Directions
• Define the allowed directions of movement (commonly down, right, up,
left):
• direction = [(1, 0), (0, 1), (-1, 0), (0, -1)]
• If all paths have been explored and no solution is found, return false.
ALGORITHM EXPLANATION:
4. Backtracking Steps
• Start at the Source Cell:
• Begin from the top-left corner of the maze (0, 0).
• Recursive Exploration:
• For the current cell (i, j):
• Check if it is the destination. If yes, return true.
• Otherwise, explore all valid neighboring cells:
• A neighboring cell (ni, nj) is valid if:
• It is within bounds of the maze.
• It is open (maze[ni][nj] = 1).
• It has not been visited yet.
• Mark the current cell as part of the path and proceed recursively.
• Backtrack When Necessary:
• If no valid move exists from the current cell, mark it as unvisited (to allow
other paths to reuse it) and backtrack to the previous cell.
• Terminate
CODE IMPLEMENTATION
#include <iostream>
#include <vector>
using namespace std;

void printSolution(const vector<vector<int>>& solution) {


for (const auto& row : solution) {
for (int cell : row) {
cout << cell << " ";
}
cout << endl;
}
}

bool isSafe(const vector<vector<int>>& maze, int x, int y, int n) {


return (x >= 0 && x < n && y >= 0 && y < n && maze[x][y] == 1);
}
CODE IMPLEMENTATION
bool solveMazeUtil(const vector<vector<int>>& maze,
vector<vector<int>>& solution, int x, int y, int n) {
if (x == n - 1 && y == n - 1 && maze[x][y] == 1) {
solution[x][y] = 1;
return true;
}

if (isSafe(maze, x, y, n)) {
solution[x][y] = 1;
if (solveMazeUtil(maze, solution, x + 1, y, n)) {
return true;
}
if (solveMazeUtil(maze, solution, x, y + 1, n)) {
return true;
}
solution[x][y] = 0;
}
return false;
}
CODE IMPLEMENTATION
bool solveMaze(const vector<vector<int>>& maze) {
int n = maze.size();
vector<vector<int>> solution(n, vector<int>(n, 0));

if (!solveMazeUtil(maze, solution, 0, 0, n)) {


cout << "No solution exists!" << endl;
return false;
}

printSolution(solution);
return true;
}

int main() {
vector<vector<int>> maze = {
{1, 0, 0, 0},
{1, 1, 0, 1},
{0, 1, 0, 0},
{1, 1, 1, 1}
};
OUTPUT

1 0 0 0
1 1 0 0
0 1 0 0
0 1 1 1

• 1 in the output marks the path taken by the rat.


• The path is (0, 0) -> (1, 0) -> (1, 1) -> (2, 1) -> (3, 1) -> (3, 2) -> (3, 3).
THANK YOU

You might also like