Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I tried to find an answer for the puzzle method ( ) where BFS uses queue, but to no avail. This BFS is used for

I tried to find an answer for the puzzle method() where BFS uses queue, but to no avail. This BFS is used for moving tiles up, down, left and right. Do you have an answer for the puzzle() method, the only function of that needs to be modified in the given code?
Here's the code I have so far:
import java.io.*;
import java.util.*;
// CSUN Spring 24 COMP469 Hwk4 xxxxxH4puz
// BFS search in 8 Puzzle problem from initial
// state to final state. Array[] is used for each state.
// Author: G. Dastghaiby Fard
public class xxxxxH4puz{
//use prt for System.out to save typing
PrintStream prt = System.out;
// Find position of Blank tile
private int findblank(int cs[]){
for (int i=0; i <9; i++)
if (cs[i]==0) return i;
// end for
return -1;
}//Return Blank Tile Location
//Move up blank tile
private int[] UPmove(int b, int a[]){
int tmp[]= a.clone();
tmp[b]= tmp[b-3]; // Swap Tiles
tmp[b-3]=0;
//prtarr(tmp,"UP");
return tmp;
}// end move up
//Move down blank tile
private int[] DNmove(int b, int a[]){
int tmp[]= a.clone();
tmp[b]=tmp[b+3]; //Swap Tiles
tmp[b+3]=0;
//prtarr(tmp,"DN");
return tmp;
}// end move down
//Move left blank tile
private int[] LTmove(int b, int a[]){
int tmp[]= a.clone();
tmp[b]=tmp[b-1];// Swap Tiles
tmp[b-1]=0;
//prtarr(tmp,"LT");
return tmp;
}// end move left
//Move right blank tile
private int[] RTmove(int b, int a[]){
int tmp[]= a.clone();
tmp[b]=tmp[b+1];// # Swap Tiles
tmp[b+1]=0;
//prtarr(tmp,"RT");
return tmp;
}// end move right
//8-puuzle from current sate to goal state
// using BFS from current sate to goal state
private void puzzle(int cs[], int gs[]){
//initialize que[]
Queue q = new LinkedList<>();
q.add(cs);
// complete the rest
}// end puzzle(cs, gs[])
private void process(String args[]){
prt.printf("
\tProcess method gets input filename from program
argument"+
"
\treads no. cases and for each case initial state and goal state"+
"
\tand for each case prints no. of moves"+
"
\tas well as all the moves from initial state to goal state.");
// local variables
int cs[], gs[]; //current and goal state
int i, j, n;
long start, end;
int cnt = args.length; // get no. of arguments
if (cnt <1){
System.out.printf("
\tOOOPS Invalid No. of aguments!");
return;
}// end if
// get input file name
String fname = args[0];
//Allocate space for initial and goal states
cs = new int[9];
gs = new int[9];
try{// open input file
Scanner inf = new Scanner(new File(fname));
n = inf.nextInt();//read no. of cases
// read initial and goal states
for (i =1; i <= n; i++){
// read current state
for (j =0; j <9; j++)
cs[j]= inf.nextInt();
// end for j
// read goal state
for (j =0; j <9; j++)
gs[j]= inf.nextInt();
// end for j
prt.printf("
\tCase %d: ", i);
//Get System Time
start = System.currentTimeMillis();
//Apply BFS from initial state and goal state
puzzle(cs, gs);
//Compute and print Exec Time
end = System.currentTimeMillis();
prt.printf("\tExec Time: %d ms", end - start);
}// end for i
// close input file
inf.close();
}catch(IOException e)
{prt.printf("
I/O Error %s", e );}
}// end process method
public static void main(String[] args) throws Exception{
System.out.printf("
\tJava program applying BFS to 8 Puzzle problem:"+
"
\t\tTo compile: javac xxxxxH4.java" +
"
\t\tTo execute: java xxxxxH4 inputfilename");
// create an instance of xxxxxH4puz class
xxxxxH4puz g = new xxxxxH4puz ();
// call process method
g.process(args);
//Write your name in next line
System.out.printf("
\tAuthor: Roman Brown Date: %s
",
java.time.LocalDate.now());
}// end main
}// end xxxxxH4puz

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

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions

Question

=+4. What is the purpose of a sandbox on a wiki? [LO-3]

Answered: 1 week ago