Question: 8-puzzle problem Modify the program code BestFS or change it to Python-3.6.9 to change the original 3x3 puzzle into a 4x4 version. BestFS.java: import java.util.ArrayList;

8-puzzle problem

Modify the program code BestFS or change it to Python-3.6.9 to change the original 3x3 puzzle into a 4x4 version.

BestFS.java:

import java.util.ArrayList;

import java.util.HashSet;

import java.util.TreeMap;

public class BestFS

{

public static void main(String[] args)

{

Config.priorityQueue = new TreeMap();

Config.existedBoard = new HashSet();

Config.solutionPath = new ArrayList();

Node root = new Node();

root.genRandBoard();

root.printBoard();

Config.existedBoard.add(root.hashBoard(root.board));

Config.priorityQueue.put(root.value, root);

while(Config.priorityQueue.size() > 0)

{

Node n = Config.priorityQueue.firstEntry().getValue();

long key = Config.priorityQueue.firstEntry().getKey();

n.genChildren();

for (int i=0; i

{

if (isEndGame(n.children.get(i)))

{

findSolution(n.children.get(i));

printSolution();

return;

}

else

{

Config.priorityQueue.put(n.children.get(i).value, n.children.get(i));

}

}

Config.priorityQueue.remove(key);

}

System.out.println("No solution");

}

private static void printSolution() {

System.out.println("Solution:");

for (int i=Config.solutionPath.size()-2; i>=0; i--)

{

Config.solutionPath.get(i).printBoard();

System.out.println();

}

System.out.println("Total " + (Config.solutionPath.size()-1) + " steps");

}

private static void findSolution(Node node)

{

Config.solutionPath.add(node);

Node p = node;

while (p.parent != null)

{

Config.solutionPath.add(p.parent);

p = p.parent;

}

}

private static boolean isEndGame(Node node)

{

return node.isGoalBoard();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!