Answered step by step
Verified Expert Solution
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 COMP Hwk xxxxxHpuz
BFS search in Puzzle problem from initial
state to final state. Array is used for each state.
Author: G Dastghaiby Fard
public class xxxxxHpuz
use prt for System.out to save typing
PrintStream prt System.out;
Find position of Blank tile
private int findblankint cs
for int i; i ; i
if csi return i;
end for
return ;
Return Blank Tile Location
Move up blank tile
private int UPmoveint b int a
int tmp aclone;
tmpb tmpb; Swap Tiles
tmpb;
prtarrtmpUP;
return tmp;
end move up
Move down blank tile
private int DNmoveint b int a
int tmp aclone;
tmpbtmpb; Swap Tiles
tmpb;
prtarrtmpDN;
return tmp;
end move down
Move left blank tile
private int LTmoveint b int a
int tmp aclone;
tmpbtmpb; Swap Tiles
tmpb;
prtarrtmpLT;
return tmp;
end move left
Move right blank tile
private int RTmoveint b int a
int tmp aclone;
tmpbtmpb; # Swap Tiles
tmpb;
prtarrtmpRT;
return tmp;
end move right
puuzle from current sate to goal state
using BFS from current sate to goal state
private void puzzleint cs int gs
initialize que
Queue q new LinkedList;
qaddcs;
complete the rest
end puzzlecs gs
private void processString args
prtprintf
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
System.out.printf
tOOOPS Invalid No of aguments!";
return;
end if
get input file name
String fname args;
Allocate space for initial and goal states
cs new int;
gs new int;
try open input file
Scanner inf new Scannernew Filefname;
n inf.nextInt;read no of cases
read initial and goal states
for i ; i n; i
read current state
for j ; j ; j
csj inf.nextInt;
end for j
read goal state
for j ; j ; j
gsj inf.nextInt;
end for j
prtprintf
tCase d: i;
Get System Time
start System.currentTimeMillis;
Apply BFS from initial state and goal state
puzzlecs gs;
Compute and print Exec Time
end System.currentTimeMillis;
prtprintftExec Time: d ms end start;
end for i
close input file
inf.close;
catchIOException e
prtprintf
IO Error s e ;
end process method
public static void mainString args throws Exception
System.out.printf
tJava program applying BFS to Puzzle problem:"
ttTo compile: javac xxxxxHjava"
ttTo execute: java xxxxxH inputfilename";
create an instance of xxxxxHpuz class
xxxxxHpuz g new xxxxxHpuz ;
call process method
gprocessargs;
Write your name in next line
System.out.printf
tAuthor: Roman Brown Date: s
java.time.LocalDate.now;
end main
end xxxxxHpuz
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