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

  • SEARCH
  • Home
  • 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 9162445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:12:49+00:00 2026-06-17T14:12:49+00:00

I have a two dimensional boolean array: boolean [][] field = new boolean[7][11]; What

  • 0

I have a two dimensional boolean array:

boolean [][] field = new boolean[7][11];

What I want to do, is check if there is a valid “path” from field[0][0] to field[7][11].

For example, this would be a valid path from start to end:
0 = false, 1 = true

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

We can connect from 0,0 to 4,4 with true values.

Example for invalid path:

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

We cannot connect fro 0,0 to 4,4. (only vertical and horizontal values are a valid path, not diagonal)

I think the best way to approach this is with recursion. Start from 0,0, check the neighbours for “true” and move on to it if so. Then again check the neighbours. If all neighbours are false, move back and find another path.

What would be the best way to approach this?

EDIT :

This is what I have so far.
The algorithm prints out exactly the correct path, but my return value is not always right.

Can anyone see where the error is?

static boolean isValidPath(boolean [][] field, int i, int j, int previ, int prevj) {

        if(i >= field.length && j >= field[0].length) return true;


        if(field[i][j] == true) {

            System.out.println("Valid i: " + i + ", j: " + j);
            if(i + 1 < field.length && i + 1 != previ) isValidPath(field, i + 1, j, i, j);
            if(j + 1 < field[0].length && j + 1 != prevj) isValidPath(field, i, j + 1, i, j);
            if(i - 1 >= 0 && i - 1 != previ) isValidPath(field, i - 1, j, i, j);
            if(j - 1 >= 0 && j - 1 != prevj) isValidPath(field, i, j - 1, i, j);

            return true;

        } else return false;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        boolean[][] field = new boolean[][] { { true,  true, false, false, false}, 
                                              { false, true, false, false, false},
                                              { true,  true, false, false, false},
                                              { true, false, false, false, false}, 
                                              { true, true, true, true, true }};

        System.out.println("Is this path valid: " + isValidPath(field, 0, 0, 0, 0));
    }

This is the output:

Valid i: 0, j: 0
Valid i: 0, j: 1
Valid i: 1, j: 1
Valid i: 2, j: 1
Valid i: 2, j: 0
Valid i: 3, j: 0
Valid i: 4, j: 0
Valid i: 4, j: 1
Valid i: 4, j: 2
Valid i: 4, j: 3
Valid i: 4, j: 4
Is this path valid: true

As you can see, the algorithm runs over the whole array, and prints out the valid path.
But if I for example change the value in [4][4] to false, the result is still true.

I dont really know where to return false and true.

  • 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-17T14:12:50+00:00Added an answer on June 17, 2026 at 2:12 pm

    What you have here is simply a graph presented in a more non-cannonical way. Any type of graph search will do to solve your problem. I suggest you use either BFS or DFS. Actually DFS usually is implemented using recursion so,yes you can solve this problem using recursion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two-dimensional array. When I print/dump this I get the following My
I have the following three arrays and need to create a new two-dimensional array
I have a two dimensional array with coordinates and i want to make the
I have a two dimensional array and I want to know how I can
I have two 2-dimensional arrays and want to filter the first array's data using
I have two-dimensional array and I want replace second of two-dimensional array on random
I have a two dimensional JSON array returned from a PHP script via a
I have a two dimensional array in php and I want to get the
i have a two dimensional array, and i want to retrieve its value in
I have two dimensional float array as below {0.2,0.0,0.3,0.0,0.0} {0.4,0.1,0.0,0.0,0.9} {0.0,0.0,0.0,0.3,0.6} I want to

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.