Answered step by step
Verified Expert Solution
Question
1 Approved Answer
FIX THE FOLLOWING CODE , make sure its working by testing the below inputs. A = 1 2 3 4 5 6 7 8 0
FIX THE FOLLOWING CODE make sure its working by testing the below inputs.
A Does reach goal state
BDoesnt reach goal state
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 board.length; i
if boardi
Check the left adjacent cell
if i && boardi
int newBoard Arrays.copyOfboard board.length;
newBoardi newBoardi;
newBoardi;
children.addnew StatenewBoard calcHeuristicnewBoard;
Check the right adjacent cell
if i board.length && boardi
int newBoard Arrays.copyOfboard board.length;
newBoardi newBoardi;
newBoardi;
children.addnew StatenewBoard calcHeuristicnewBoard;
Move between positions &
if i && boardi && board
int newBoard Arrays.copyOfboard board.length;
int newPosition i : ;
newBoardnewPosition newBoardi;
newBoardi;
children.addnew StatenewBoard calcHeuristicnewBoard;
Move between positions &
if i && boardi && board
int newBoard Arrays.copyOfboard board.length;
int newPosition i : ;
newBoardnewPosition newBoardi;
newBoardi;
children.addnew StatenewBoard calcHeuristicnewBoard;
Move between positions &
if i && boardi && board
int newBoard Arrays.copyOfboard board.length;
int newPosition i : ;
newBoardnewPosition newBoardi;
newBoardi;
children.addnew StatenewBoard calcHeuristicnewBoard;
return children;
private int calcHeuristicint board
int disp ;
for int i ; i board.length; i
if boardi i
disp;
return disp;
public void displayStateFileWriter output throws IOException
output.writeh hValue
;
for int i ; i board.length; i
output.writeboardi;
output.write
;
public class TEST
public static void mainString args
try
File file new FileinputCtxt;
Scanner sc new Scannerfile;
int iniState new int;
for int i ; i ; i
iniStatei scnextInt;
State initialState new StateiniState calcHeuristiciniState;
BFSinitialState;
scclose;
catch FileNotFoundException e
eprintStackTrace;
BFS Algorithm
private static void BFSState initialState
PriorityQueue frontier new PriorityQueueComparatorcomparingIntState::getHValue;
frontier.addinitialState;
Set visited new HashSet;
int moves ;
try
FileWriter output new FileWriteroutputtxt;
while frontier.isEmpty
State currState frontier.poll;
visited.addcurrState;
currState.displayStateoutput;
if currStategetHValue
output.writeGoal state reached.
Number of moves: moves;
output.close;
return;
for State child : currState.generateChildren
if visited.containschild && frontier.containschild
frontier.addchild;
if moves
output.write
Max no of moves reached"
return;
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