Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
8 Puzzle in Java Using Bredth-First Search

Ok so I understand the Bredth First Search Iterates nodes and goes to the next one that is the largest. I also have no idea how exactly that relates to the 8 Puzzle Game. Here is my code:

public static void breadthFirstSearch(int[] num, int m, Vector solution) {
    Queue t = new PriorityQueue();
    ArrayList Visited = new ArrayList();
    blankNode = 0;
    int temp;
    printArray(num);
    for (int i : num) {
        if (num[i] == m) {
            blankNode = i;
            System.out.println(blankNode);
        }
    }


    System.out.println(blankNode);
    printArray(num);

}

private static void printArray(int[] num) {
    for (int i : num) {
        System.out.print(num[i] + ",");
    }
    System.out.println();
}

private static int[] moveUp(int[] num, int blankSpace) {
    int temp = 0;
    if (blankSpace != 6 && blankSpace != 1 && blankSpace != 2) {
        blankNode = blankSpace - 3;
        temp = num[blankSpace - 3];
        num[blankSpace - 3] = num[blankSpace];
        num[blankSpace] = temp;
    }

    return num;
}

private static int[] moveLeft(int[] num, int blankSpace) {
    int temp;
    if (blankSpace != 0 && blankSpace != 3 && blankSpace != 6) {
        blankNode = blankSpace - 1;
        temp = num[blankSpace - 1];
        num[blankSpace - 1] = num[blankSpace];
        num[blankSpace] = temp;
    }
    return num;
}

private static int[] moveDown(int[] num, int blankSpace) {
    int temp;
    if (blankSpace != 6 && blankSpace != 7 && blankSpace != 8) {
        blankNode = blankSpace + 3;
        temp = num[blankSpace + 3];
        num[blankSpace + 3] = num[blankSpace];
        num[blankSpace] = temp;
    }
    return num;
}

private static int[] moveRight(int[] num, int blankSpace) {
    int temp;
    if (blankSpace != 2 && blankSpace != 5 && blankSpace != 8) {
        blankNode = blankSpace + 1;
        temp = num[blankSpace + 1];
        num[blankSpace + 1] = num[blankSpace];
        num[blankSpace] = temp;
    }
    return num;
}

Now, in the method breadthFirstSearch I need to play tiles with the num array. So imagine the num array as

045
789
312

and the blank node is 7, I need to get it down to the bottom right corner by only swapping it with numbers above below to the right or left of it. m gives me the number that is blank. How can breath first search be used here to do this? Do I just need to check every possible combination and choose the correct one? Please help.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!