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 6873527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:04:48+00:00 2026-05-27T04:04:48+00:00

I am solving an NxN puzzle in java 2D array. I can move to

  • 0

I am solving an NxN puzzle in java 2D array. I can move to the empty tile in say four directions : LEFT, RIGHT, DOWN or UP. My question is if i get the indices(row en column values) of the empty tiles(node), how can i know if i must move LEFT, RIGHT, DOWN or UP to generate its successor(neighbour) nodes.

For instance if it is a 1Dimensional array of 9 elements then i can do something like: index is the index of the empty tile

if(index == 0){
            tempSuccessorNodes.add(new Node(swap(0,1,arrayPosition),curNode));
            tempSuccessorNodes.add(new Node(swap(0,3,arrayPosition),curNode));
        }
        else if(index == 1){
            tempSuccessorNodes.add(new Node(swap(0,1,arrayPosition),curNode));
            tempSuccessorNodes.add(new Node(swap(1, 4, arrayPosition),curNode));
            tempSuccessorNodes.add(new Node(swap(1, 2, arrayPosition),curNode));
        }

        ....

         if(index == 8){
            tempSuccessorNodes.add(new Node(swap(8, 7, arrayPosition),curNode));
            tempSuccessorNodes.add(new Node(swap(8, 5, arrayPosition),curNode));
        }

to generate the successors of the current node. But here am dealing with NxN(which 3×3 is an instance). How do i know if i must move LEFT, RIGHT, DOWN or UP after knowing the indices of the empty cell/tile/node?

I have a question here i posted earlier which relates to the same task am handling

Thanks

  • 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-05-27T04:04:48+00:00Added an answer on May 27, 2026 at 4:04 am

    You’ll have to take two things into account, to determine in which direction you can move:

    1. Whether you have already visited one of the neighboring tiles (otherwise, you risk entering an infinite loop). For that, each time you want to visit a tile you must check first if that tile has already been visited; if not add it to a set and visit it, otherwise ignore it. Notice that checking is easy, simply ask if the tile is already in the set.
    2. For determining the neighbors of each tile, you must check four possible edge cases, in all other cases it’s safe to visit the tiles:
      • if the tile is in the first row, don’t go visiting the previous row
      • if the tile is in the last row, don’t go visiting the next row
      • if the tile is in the first column, don’t go visiting the previous column
      • if the tile is in the last column, don’t go visiting the next column
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am solving a NxN puzzle in Java 2D array ( int state[][] )
I've had a go at solving the Eternity puzzle (1,000,000 GBP prize) and Netflix
This is my first effort on solving the exercise. I gotta say, I'm kind
My problem is conceptually similar to solving anagrams, except I can't just use a
I have the following problem in my Data Structures and Problem Solving using Java
I am solving Programming Pearls exercises. 4.11 say: Write and prove the correctness of
I am solving situation about printing data from array via each loop. in my
I was solving some old exams of the first major exam in Java. I
I am solving this problem of rotating an array and got algorithm and code
I'm solving UVA's Edit Step Ladders on an uva sub-site named programming-challenges.com, but since

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.