Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:25:38+00:00 2026-06-13T00:25:38+00:00

This is my first time implementing Dijkstra’s algorithm. Okay so here I have a

  • 0

This is my first time implementing Dijkstra’s algorithm. Okay so here I have a simple 2D 9 by 9 array:

9 by 9 array

Starting point is 1 and we’re trying to get to any green square. Red squares are walls or lava (whatever satisfies your imagination).

How do I implement this in Java?

Computer science is not my field hence I’m not a seasoned programmer so I might not know how to do some stack pushing, only loops and recursion 🙁 please keep it easy as possible and bear with me!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:25:39+00:00Added an answer on June 13, 2026 at 12:25 am

    Here’s something similiar that should get you started. However, the solution presented below attempts to get to the bottom right corner. You can relax that condition to find the bottom row. You will also need to change the encoding slightly to have a unique value that represents this row.

    public class MazeSolver {
    
        final static int TRIED = 2;
        final static int PATH = 3;
    
        // @formatter:off
        private static int[][] GRID = { 
            { 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1 },
            { 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1 },
            { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0 },
            { 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1 },
            { 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1 },
            { 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } 
        };
        // @formatter:off
    
        public static void main(String[] args) {
            MazeSolver maze = new MazeSolver(GRID);
            boolean solved = maze.solve();
            System.out.println("Solved: " + solved);
            System.out.println(maze.toString());
        }
    
        private int[][] grid;
        private int height;
        private int width;
    
        private int[][] map;
    
        public MazeSolver(int[][] grid) {
            this.grid = grid;
            this.height = grid.length;
            this.width = grid[0].length;
    
            this.map = new int[height][width];
        }
    
        public boolean solve() {
            return traverse(0,0);
        }
    
        private boolean traverse(int i, int j) {
            if (!isValid(i,j)) {
                return false;
            }
    
            if ( isEnd(i, j) ) {
                map[i][j] = PATH;
                return true;
            } else {
                map[i][j] = TRIED;
            }
    
            // North
            if (traverse(i - 1, j)) {
                map[i-1][j] = PATH;
                return true;
            }
            // East
            if (traverse(i, j + 1)) {
                map[i][j + 1] = PATH;
                return true;
            }
            // South
            if (traverse(i + 1, j)) {
                map[i + 1][j] = PATH;
                return true;
            }
            // West
            if (traverse(i, j - 1)) {
                map[i][j - 1] = PATH;
                return true;
            }
    
            return false;
        }
    
        private boolean isEnd(int i, int j) {
            return i == height - 1 && j == width - 1;
        }
    
        private boolean isValid(int i, int j) {
            if (inRange(i, j) && isOpen(i, j) && !isTried(i, j)) {
                return true;
            }
    
            return false;
        }
    
        private boolean isOpen(int i, int j) {
            return grid[i][j] == 1;
        }
    
        private boolean isTried(int i, int j) {
            return map[i][j] == TRIED;
        }
    
        private boolean inRange(int i, int j) {
            return inHeight(i) && inWidth(j);
        }
    
        private boolean inHeight(int i) {
            return i >= 0 && i < height;
        }
    
        private boolean inWidth(int j) {
            return j >= 0 && j < width;
        }
    
        public String toString() {
            String s = "";
            for (int[] row : map) {
                s += Arrays.toString(row) + "\n";
            }
    
            return s;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first time asking a question here. I tried to be well
This is my first time using XML documents. What I'm trying to do is
This is my first time here and I am a beginner so kindly bear
This is my first time trying to set up a database. My intent is
This is my first time implementing In App Purchases. During development and testing of
I am Implementing ace into my site for the first time and I have
This is the first time I am implementing in-app billing in android app and
Got this error for the first time, I have looked around and cannot find
I have just installed a custom WordPress theme (this is my first time working
This is the first time i'm asking a question here at stack overflow so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.