Question: Need to create solveBFS ( ) method ( use queue ) & test it . Given: import java.io . BufferedReader; import java.io . FileReader; import
Need to create solveBFS method use queue & test it Given:
import java.ioBufferedReader;
import java.ioFileReader;
import java.ioFileWriter;
import java.ioIOException;
import java.ioPrintWriter;
import java.util.Stack;
import java.util.LinkedList;
import java.util.Queue;
import java.util.ArrayList;
public class Pacman
private Node maze;
private String inputFileName;
private String outputFileName;
private int height;
private int width;
private int startX;
private int startY;
private static class Node
private char content;
private int row;
private int col;
private boolean visited;
private Node parent;
public Nodeint x int y char c
visited false;
content c;
parent null;
this.row x;
this.col y;
public boolean isWall return content X;
public boolean isVisited return visited;
public PacmanString inputFileName, String outputFileName
this.inputFileName inputFileName;
this.outputFileName outputFileName;
buildGraph;
private boolean inMazeint index, int bound
return index bound && index ;
private void backtrackNode end
Node current end;
while current null
if currentcontent S && current.content G
current.content ;
current current.parent;
public void writeOutput
try
PrintWriter output new PrintWriternew FileWriteroutputFileName;
output.printlnheight width;
for int i ; i height; i
StringBuilder line new StringBuilder;
for int j ; j width; j
char content mazeijcontent;
if content
line.append;
else
line.appendcontent;
output.printlnlinetoString;
output.close;
catchIOException e
eprintStackTrace;
public String toString
String s ;
s height width
;
for int i ; i height; i
for int j ; j width; j
s mazeijcontent ;
s
;
return s;
private void buildGraph
try
BufferedReader input new BufferedReadernew FileReaderinputFileName;
int i ;
int j ;
String inputstr input.readLine;
String strarray inputstrsplit;
height Integer.parseIntstrarray;
width Integer.parseIntstrarray;
maze new Nodeheightwidth;
for i; i height; i
inputstr input.readLine;
for j ; j width; j
mazeij new Nodei j inputstrcharAtj;
if inputstrcharAtjS
startX i;
startY j;
input.close;
catch IOException e
eprintStackTrace;
public ArrayList getNeighborsNode currentNode
Node north, south, east, west;
ArrayList nbors new ArrayList;
int row currentNode.row;
int col currentNode.col;
north mazerow col;
south mazerow col;
west mazerowcol ;
east mazerowcol ;
if inMazerow height &&
north.isWall &&
north.isVisited
nbors.addnorth;
if inMazerow height &&
south.isWall &&
south.isVisited
nbors.addsouth;
if inMazecol width &&
west.isWall &&
west.isVisited
nbors.addwest;
if inMazecol width &&
east.isWall &&
east.isVisited
nbors.addeast;
return nbors;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
