Skip to content

### FIxes<Mazerecursion> - #7705

Open
krishnayak986-lang wants to merge 1 commit into
TheAlgorithms:masterfrom
krishnayak986-lang:fix/maze-recursion-bounds-and-validation
Open

krishnayak986-lang wants to merge 1 commit into
TheAlgorithms:masterfrom
krishnayak986-lang:fix/maze-recursion-bounds-and-validation

Conversation

@krishnayak986-lang

Copy link
Copy Markdown
  • [x ] I have read CONTRIBUTING.md.
  • [ x] This pull request is all my own work -- I have not plagiarized it.
  • [ x] All filenames are in PascalCase.
  • [ x] All functions and variable names follow Java naming conventions.
  • [ x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • [ x] All new algorithms include a corresponding test class that validates their functionality.
  • [ x] All new code is formatted with clang-format -i --style=file path/to/your/file.java

Description

This PR improves MazeRecursion so it no longer crashes on small mazes, supports custom start and target positions, validates its input, and no longer duplicates the solving logic.

Problem

  • solveMazeUsingFirstStrategy and solveMazeUsingSecondStrategy always read the hard-coded destination map[6][5], so any maze smaller than that (for example 2x2) threw an ArrayIndexOutOfBoundsException.
  • The start (1,1) and destination (6,5) were hard-coded, so the solver couldn't be used with mazes of other sizes.
  • setWay and setWay2 were nearly identical and differed only in direction order.

Changes

  • Added a bounds check before any cell is accessed, so the solver can no longer go out of bounds.
  • Added overloads of both solve methods that accept startRow, startCol, targetRow, targetCol. The existing single-argument methods are kept and still use start (1,1) and target (6,5).
  • Added input validation. An IllegalArgumentException is thrown for a null or empty maze, jagged rows, or a start/target position outside the maze.
  • Replaced setWay and setWay2 with one shared recursive method that takes the direction order as a parameter.
  • Preserved both traversal orders:
    • First strategy: down -> right -> up -> left
    • Second strategy: up -> right -> down -> left
  • Paths are still marked 2 and dead ends 3. A maze with no route still returns null.

Tests

Added tests to MazeRecursionTest covering:

  • A small maze solved with a custom start and target
  • Both strategies keeping their traversal order
  • A small maze with the default target throwing IllegalArgumentException instead of ArrayIndexOutOfBoundsException
  • Null, empty and jagged mazes
  • Out-of-bounds start and target
  • An unsolvable maze returning null

The existing tests for the default 8x7 maze are unchanged and still pass.

Design note

For invalid input (including a default target outside a small maze) I chose to throw IllegalArgumentException, while "no route found" keeps returning null as before. If maintainers prefer returning null for invalid input, that is a small change in validate, so let me know.

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.

1 participant