Answered step by step
Verified Expert Solution
Question
1 Approved Answer
USING THE INSTRUCTIONS FROM THE PICTURE BELOW , FIX THE CODE BELOW THE PICTURE AND ALSO SHOW RESULTS OF WHEN THE GOAL STATE IS REACHED.
USING THE INSTRUCTIONS FROM THE PICTURE BELOW FIX THE CODE BELOW THE PICTURE AND ALSO SHOW RESULTS OF WHEN THE GOAL STATE IS REACHED.
Nine Little Soldiers is a puzzle in which numbered soldiers are trapped in a bomb shelter that
has cells as shown in the figure below. A cell's number is shown either below or above the
cell, and the soldiers are identified by the numbers inside a circle.
A soldier may only move into an adjacent empty cell and no more than one soldier may occupy a
single cell. The puzzle requires you to move the soldiers so that soldier is in cell number
soldier in cell number and so on up to soldier in cell number
The figure below depicts start state CODE: import java.io;
import java.util.;
class State implements Comparable
private int hValue;
private int board;
public Stateint board, int hValue
this.hValue hValue;
this.board board;
@Override
public int compareToState o
return Integer.comparethishValue, ohValue;
public int getHValue
return hValue;
List generateChildren
List children new ArrayList;
for int i ; i ; i
if boardi
if boardi
int newBoard Arrays.copyOfboard board.length;
newBoardi newBoardi;
newBoardi;
children.addnew StatenewBoard calculateHeuristicnewBoard;
if boardi
int newBoard Arrays.copyOfboard board.length;
newBoardi newBoardi;
newBoardi;
children.addnew StatenewBoard calculateHeuristicnewBoard;
return children;
private int calculateHeuristicint board
int displaced ;
for int i ; i ; i
if boardi i
displaced;
return displaced;
void printStateFileWriter output throws IOException
output.write
h hValue
;
for int i ; i board.length; i
output.writeboardi;
output.write
;
public class NineSoldiers
public static void mainString args
try
File file new FileC:UsersVukosiIdeaProjectsuntitledsrcinputBtxt;
Scanner sc new Scannerfile;
int initialState new int;
for int i ; i ; i
initialStatei scnextInt;
State startState new StateinitialState startStateHeuristicinitialState;
bestFirstSearchstartState;
scclose;
catch FileNotFoundException e
eprintStackTrace;
private static void bestFirstSearchState startState
PriorityQueue frontier new PriorityQueueComparatorcomparingInts sgetHValue;
frontier.addstartState;
Set explored new HashSet;
int moves ;
try
FileWriter output new FileWriteroutputAtxt;
while frontier.isEmpty
State currentState frontier.poll;
explored.addcurrentState;
currentState.printStateoutput;
if currentStategetHValue
System.out.printlnGoal state reached.";
output.writeGoal state reached.
;
output.writeNumber of moves: moves;
output.close;
return;
for State child : currentState.generateChildren
if explored.containschild && frontier.containschild
frontier.addchild;
if moves
Systemout.printlnMaximum number of moves reached.";
output.write
Maximum number of moves reached.
;
output.close;
return;
moves;
output.close;
catch IOException e
eprintStackTrace;
private static int startStateHeuristicint board
int displaced ;
for int i ; i ; i
if boardi i displaced;
return displaced;
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