Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How would I write the code for the stack and queue in this code import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; //starter code for MazeSolver //CST-201

How would I write the code for the stack and queue in this code

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;

//starter code for MazeSolver //CST-201

public class Driver {

/** * * @param start * @param end * find a path through the maze * if found, output the path from start to end * if not path exists, output a message stating so * */ // implement this method public static void depthFirst(MazeCell start, MazeCell end) { }

public static void main(String[] args) throws FileNotFoundException { //create the Maze from the file Scanner fin = new Scanner(new File("Maze.in")); //read in the rows and cols int rows = fin.nextInt(); int cols = fin.nextInt(); //create the maze int [][] grid = new int[rows][cols]; //read in the data from the file to populate for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { grid[i][j] = fin.nextInt(); } }

//look at it for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (grid[i][j] == 3) System.out.print("S "); else if (grid[i][j] == 4) System.out.print("E "); else System.out.print(grid[i][j] + " "); } System.out.println(); }

//make a 2-d array of cells MazeCell[][] cells = new MazeCell[rows][cols]; //populate with MazeCell obj - default obj for walls

MazeCell start = new MazeCell(), end = new MazeCell(); //iterate over the grid, make cells and set coordinates for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { //make a new cell cells[i][j] = new MazeCell(); //if it isn't a wall, set the coordinates if (grid[i][j] != 0) { cells[i][j].setCoordinates(i, j); //look for the start and end cells if (grid[i][j] == 3) start = cells[i][j]; if (grid[i][j] == 4) end = cells[i][j]; }

} } //testing System.out.println("start:"+start+" end:"+end); //solve it! //depthFirst(start, end); } }

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_2

Step: 3

blur-text-image_3

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

Elementary Principles of Chemical Processes

Authors: Richard M. Felder, Ronald W. Rousseau

3rd Edition

978-0471687573, 9788126515820, 978-0-471-4152, 0471720631, 047168757X, 8126515821, 978-0471720638

More Books

Students also viewed these Programming questions