-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeSolver.java
More file actions
88 lines (76 loc) · 2.89 KB
/
MazeSolver.java
File metadata and controls
88 lines (76 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.*;
import java.io.*;
public class MazeSolver {
public static void main(String[] args) throws IOException {
//----------FILE INPUT-----------------------------
File myFile = new File("samp.txt");
Scanner input = new Scanner (myFile);
int numRows = input.nextInt();
int numCols = input.nextInt();
input.nextLine();
//----------SET START------------------------------
int startX = 0;
int startY = 0;
//----------SET MAZE-------------------------------
char[][] maze = new char[numRows][numCols];
//----------PRINT MAZE-----------------------------
for (int i = 0; i < numRows; i++)
{
String nextLine = input.nextLine();
for (int j = 0; j < numCols; j++)
{
char nextChar = nextLine.charAt(j);
maze[i][j] = nextChar;
System.out.print(nextChar);
}
System.out.println();
}
//----------FIND START POINT-----------------------
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
if (maze[i][j] == '+')
{
System.out.println("Starting coordinates: " + i + ", " + j);
startX = i;
startY = j;
}
}
}
//----------FIND END POINT------------------------
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
if (maze[i][j] == '-')
{
System.out.println("End coordinates: " + i + ", " + j);
}
}
}
//----------START TIMER--------------------------
long starttimer = System.currentTimeMillis();
//----------SOLVE MAZE---------------------------
MazeEngine newMaze = new MazeEngine(maze);
System.out.println();
newMaze.findPath(startX, startY);
newMaze.printMaze(numRows, numCols);
//----------END TIMER----------------------------
long stoptimer = System.currentTimeMillis();
long elapsed = stoptimer - starttimer;
System.out.println("Solution found in: " + elapsed + " milliseconds");
//----------PRINT RESULTS TO FILE----------------
char [][] a = newMaze.fileout();
PrintWriter out = new PrintWriter("Answer.txt");
for(int i = 0; i< numRows; i++)
{
for(int j=0; j< numCols; j++)
{
out.print(a[i][j]);
} out.println();
}
out.close();
} //Main
}//Class }
}