Answered step by step
Verified Expert Solution
Link Copied!

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 =1234567809000(Does reach goal state)
B=0234567891000(Doesnt reach goal state)
import java.io.*;
import java.util.*;
class State implements Comparable {
private int hValue;
private int[] board;
public State(int[] board, int hValue){
this.hValue = hValue;
this.board = board;
}
@Override
public int compareTo(State o){
return Integer.compare(this.hValue, o.hValue);
}
public int getHValue(){
return hValue;
}
List generateChildren(){
List children = new ArrayList<>();
for (int i =0; i < board.length; i++){
if (board[i]!=0){
// Check the left adjacent cell
if (i -1>=0 && board[i -1]==0){
int[] newBoard = Arrays.copyOf(board, board.length);
newBoard[i -1]= newBoard[i];
newBoard[i]=0;
children.add(new State(newBoard, calcHeuristic(newBoard)));
}
// Check the right adjacent cell
if (i +1< board.length && board[i +1]==0){
int[] newBoard = Arrays.copyOf(board, board.length);
newBoard[i +1]= newBoard[i];
newBoard[i]=0;
children.add(new State(newBoard, calcHeuristic(newBoard)));
}
// Move between positions 4 & 11
if ((i ==3 && board[10]==0)||(i ==10 && board[3]==0)){
int[] newBoard = Arrays.copyOf(board, board.length);
int newPosition =(i ==3)?10 : 3;
newBoard[newPosition]= newBoard[i];
newBoard[i]=0;
children.add(new State(newBoard, calcHeuristic(newBoard)));
}
// Move between positions 6 & 12
if ((i ==5 && board[11]==0)||(i ==11 && board[5]==0)){
int[] newBoard = Arrays.copyOf(board, board.length);
int newPosition =(i ==5)?11 : 5;
newBoard[newPosition]= newBoard[i];
newBoard[i]=0;
children.add(new State(newBoard, calcHeuristic(newBoard)));
}
// Move between positions 8 & 13
if ((i ==7 && board[12]==0)||(i ==12 && board[7]==0)){
int[] newBoard = Arrays.copyOf(board, board.length);
int newPosition =(i ==7)?12 : 7;
newBoard[newPosition]= newBoard[i];
newBoard[i]=0;
children.add(new State(newBoard, calcHeuristic(newBoard)));
}
}
}
return children;
}
private int calcHeuristic(int[] board){
int disp =0;
for (int i =0; i < board.length; i++){
if (board[i]!= i)
disp++;
}
return disp;
}
public void displayState(FileWriter output) throws IOException {
output.write("h ="+ hValue +"
");
for (int i =0; i < board.length; i++)
output.write(board[i]+"");
output.write("
");
}
public class TEST {
public static void main(String[] args){
try {
File file = new File("inputC.txt");
Scanner sc = new Scanner(file);
int[] iniState = new int[14];
for (int i =0; i <10; i++)
iniState[i]= sc.nextInt();
State initialState = new State(iniState, calcHeuristic(iniState));
BFS(initialState);
sc.close();
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
// BFS Algorithm
private static void BFS(State initialState){
PriorityQueue frontier = new PriorityQueue<>(Comparator.comparingInt(State::getHValue));
frontier.add(initialState);
Set visited = new HashSet<>();
int moves =0;
try {
FileWriter output = new FileWriter("output.txt");
while (!frontier.isEmpty()){
State currState = frontier.poll();
visited.add(currState);
currState.displayState(output);
if (currState.getHValue()==0){
output.write("Goal state reached.
Number of moves: "+ moves);
output.close();
return;
}
for (State child : currState.generateChildren()){
if (!visited.contains(child) && !frontier.contains(child))
frontier.add(child);
}
if (moves >=700){
output.write("
Max no of moves reached")
return;}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2022 Workshops 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 In Computer And Information Science 33

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Alfred Taudes ,Atif Mashkoor ,Johannes Sametinger ,Jorge Martinez-Gil ,Florian Sobieczky ,Lukas Fischer ,Rudolf Ramler ,Maqbool Khan ,Gerald Czech

1st Edition

ISBN: 3031143426, 978-3031143427

More Books

Students also viewed these Databases questions

Question

How does the concept of hegemony relate to culture?

Answered: 1 week ago