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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:43:29+00:00 2026-05-26T22:43:29+00:00

I am solving a NxN puzzle in Java 2D array ( int state[][] )

  • 0

I am solving a NxN puzzle in Java 2D array (int state[][]) and I am stuck.

I am using the Manhattan distance heuristic. The problem I have now is how to code the successors of a node. I have seen numerous examples on the net how a node’s successors (neighbours) are generated but they are all in 1 dimensional array. I can’t find any tutorial that teaches generating the successors of a node in 2D arrays.

How can I code to generate the successors of a node? A node can move to the empty space to the LEFT, RIGHT, DOWN or UP. The size of the board is NxN.

It is my first time using 2D arrays to program game like these puzzles.

  • 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-26T22:43:30+00:00Added an answer on May 26, 2026 at 10:43 pm

    Probably the code you have defines

    UP as ( i + width * (j-1))
    DOWN as ( i + width * (j+1))
    LEFT as ( i - 1 + width * j)
    RIGHT as (i + 1 + width *j)
    

    You need to define these as (i, j-1), (i, j+1), (i-1, j), (i+1, j) respectively.

    Edit:

    In a 1D array, you need to flatten the element index. If the element is at column i, row j, flat address would be ( j*width [meaning skip j rows] + i [meaning skip i items in this row]). If you have a 2D array then you can simple use array[i][j] instead of a flat address.

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

Sidebar

Related Questions

I have the following problem in my Data Structures and Problem Solving using Java
I am solving an NxN puzzle in java 2D array. I can move to
After solving the other problem with routes , now I have another one. I
I need some help in solving this problem. We have a large amount of
I'm having trouble solving this problem. I have to find all simple paths starting
I am solving the following problem: Suppose I have a list of software packages
I have tried solving this problem by posting other related questions here that focused
I am solving following problem - I have on my page statement of items
I'm solving a much larger problem and have run into a bug when I
Continue solving this problem , i've found couple of 'org.hibernate.impl.SessionFactoryImpl' memory leaks using MAT

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.