GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ### FIxes<Mazerecursion> by krishnayak986-lang · Pull Request #7705 · TheAlgorithms/Java

Conversation

@krishnayak986-lang

@krishnayak986-lang krishnayak986-lang commented Oct 30, 2026

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@krishnayak986-lang
fix: handle bounds, validation and custom start/target in MazeRecursion
df3b5d6
@krishnayak986-lang
krishnayak986-lang requested review from DenizAltunkapan, alxkm and yanglbme as code owners October 30, 2026 10:25
krishnayak986-lang added 3 commits November 12, 2026 20:08
@krishnayak986-lang
Merge upstream master and resolve conflicts
a90b3e5
@krishnayak986-lang
style: apply clang-format
37b1071
@krishnayak986-lang
test: fix formatting and use fresh maze per strategy
a6ede95
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@DenizAltunkapan DenizAltunkapan Awaiting requested review from DenizAltunkapan DenizAltunkapan is a code owner
@yanglbme yanglbme Awaiting requested review from yanglbme yanglbme is a code owner
@alxkm alxkm Awaiting requested review from alxkm alxkm is a code owner

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@krishnayak986-lang
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.