Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using this code only. How would I scan a .dat file for it to be the maze instead of manually putting the values of the

Using this code only. How would I scan a .dat file for it to be the maze instead of manually putting the values of the maze on the code? Also, how would I print the maze also? The top of the .dat file is the number of rows and columns. Java. Thank you.

basicMaze.dat file:

10 10 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 1 1 0 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 0 0

Code:

import java.util.*; import java.util.Scanner; class GFG { static int ROW = 9; static int COL = 10; static class Point { int x; int y; public Point(int x, int y) { this.x = x; this.y = y; } }; static class queueNode { Point pt; int dist; public queueNode(Point pt, int dist) { this.pt = pt; this.dist = dist; } }; static boolean isValid(int row, int col) { return (row >= 0) && (row < ROW) && (col >= 0) && (col < COL); } static int rowNum[] = {-1, 0, 0, 1}; static int colNum[] = {0, -1, 1, 0}; static int BFS(int mat[][], Point src, Point dest) { if (mat[src.x][src.y] != 1 || mat[dest.x][dest.y] != 1) return -1; boolean [][]visited = new boolean[ROW][COL]; visited[src.x][src.y] = true; Queue q = new LinkedList<>(); queueNode s = new queueNode(src, 0); q.add(s); while (!q.isEmpty()) { queueNode curr = q.peek(); Point pt = curr.pt; if (pt.x == dest.x && pt.y == dest.y) return curr.dist; q.remove(); for (int i = 0; i < 4; i++) { int row = pt.x + rowNum[i]; int col = pt.y + colNum[i]; if (isValid(row, col) && mat[row][col] == 1 && !visited[row][col]) { visited[row][col] = true; queueNode Adjcell = new queueNode (new Point(row, col), curr.dist + 1 ); q.add(Adjcell); } } } return -1; } public static void main(String[] args) { int mat[][] = {{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 }, { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 }, { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, { 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 }, { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }, { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, { 1, 1, 0, 0, 0, 0, 1, 0, 0, 1 }}; Point source = new Point(0, 0); Point dest = new Point(3, 4); int dist = BFS(mat, source, dest); if (dist != -1) System.out.println("Shortest Path is " + dist); else System.out.println("Shortest Path doesn't exist"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

To read the maze from a dat file and print it you can modify the code to utilize Scanner for file in... 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

Financial Management for Public Health and Not for Profit Organizations

Authors: Steven A. Finkler, Thad Calabrese

4th edition

133060411, 132805669, 9780133060416, 978-0132805667

More Books

Students also viewed these Programming questions

Question

What are some of the features of the Unified Process (UP)?

Answered: 1 week ago

Question

What are some common causes of variances?

Answered: 1 week ago

Question

What is the difference between periodic and perpetual inventory?

Answered: 1 week ago

Question

Explain the monetary denominator concept.

Answered: 1 week ago