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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started