Skip to content

feat: add Rat in a Maze backtracking algorithm#7418

Merged
DenizAltunkapan merged 4 commits into
TheAlgorithms:masterfrom
the-Sunny-Sharma:feature/rat-in-a-maze
May 13, 2026
Merged

feat: add Rat in a Maze backtracking algorithm#7418
DenizAltunkapan merged 4 commits into
TheAlgorithms:masterfrom
the-Sunny-Sharma:feature/rat-in-a-maze

Conversation

@the-Sunny-Sharma
Copy link
Copy Markdown
Contributor

Description

Adds a Rat in a Maze implementation using the backtracking technique.

Given an n x n binary maze where 1 = open and 0 = blocked, the
algorithm finds all paths from the top-left (0,0) to the bottom-right
(n-1,n-1) cell. The rat can move in four directions: Down, Left, Right, Up.

Implementation Details

  • File: src/main/java/com/thealgorithms/backtracking/RatInAMaze.java
  • Approach: Depth-first backtracking with a visited matrix to avoid
    revisiting cells; explores directions in alphabetical order (D, L, R, U)
  • Returns: List of all valid path strings (e.g., "DDRDRR")
  • Time Complexity: O(4^(n²)) worst case
  • Space Complexity: O(n²) for visited matrix and recursion stack

Checklist

  • Added implementation in correct package com.thealgorithms.backtracking
  • final class with private constructor following repo conventions
  • Full Javadoc on class and all public/private methods
  • Input validation with IllegalArgumentException for null/empty/non-square maze
  • 10 unit tests covering: multiple paths, single path, no path, source
    blocked, destination blocked, single cell open, single cell blocked,
    null maze, empty maze, non-square maze
  • All 10 tests pass

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.55%. Comparing base (e814d97) to head (26a7b93).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7418      +/-   ##
============================================
+ Coverage     79.53%   79.55%   +0.01%     
- Complexity     7177     7197      +20     
============================================
  Files           798      799       +1     
  Lines         23474    23501      +27     
  Branches       4617     4624       +7     
============================================
+ Hits          18671    18697      +26     
  Misses         4055     4055              
- Partials        748      749       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DenizAltunkapan DenizAltunkapan merged commit e7f8979 into TheAlgorithms:master May 13, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants