Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
143 lines (116 loc) · 4.54 KB
/
Copy pathMain.java
File metadata and controls
143 lines (116 loc) · 4.54 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.util.LinkedList;
import java.util.Queue;
import java.util.HashSet;
import java.util.Random;
/* @About
* the program is used to find the shortest path on the grid from the beginning (1) to the target (4)
* Algorithm used: BFS (breadth-first search)
* Coordinates (target, beginning, obstacles ) are randomly generated
*/
public class Main {
int[][] map = new int[22][10]; // initial grid, which we will modify
int step = 220; // to find the least path
int total_paths = 0; // to keep track of total number of possible paths
// to print the grid
public static void printing(int[][] map) { // Printing the desk
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
// to clone a matrix (2D array)
public static int[][] deepCopy(int[][] original) {
if (original == null) {
return null;
}
int[][] copy = new int[original.length][];
for (int i = 0; i < original.length; i++) {
// Manual deep copy for each sub-array (row)
copy[i] = new int[original[i].length];
System.arraycopy(original[i], 0, copy[i], 0, original[i].length);
}
return copy;
}
// the main code for the algorithm
public void engine(int tx, int ty, int hx, int hy, int ox1, int oy1, int ox2, int oy2, int ox3, int oy3) {
// initial grid preparation
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
map[i][j] = 0;
}
}
map[ty][tx] = 4; // target
map[hy][hx] = 1; // worm head
map[oy1][ox1] = 3; // obstacle 1
map[oy2][ox2] = 3; // obstacle 2
map[oy3][ox3] = 3; // obstacle 3
// Printing the initial matrix
System.out.println("Printing the initial matrix");
printing(map);
int[] dx = {0, 0, 1, -1};
int[] dy = {1, -1, 0, 0};
// initialization of main variables
Queue<Pointer> q = new LinkedList<>(); // Queue to visit each coordinate
HashSet<String> hs = new HashSet<String>(); // to store previously visited coordinations
int[][] final_map = null; // final grid (map)
q.add(new Pointer(hy, hx, 0, map)); // add the first coordinate to the queue
while(!q.isEmpty()) { // used BFS (breath-first search) to find the shortest path
Pointer temp = q.poll();
for(int i = 0; i < 4; i++) {
if((temp.x + dx[i] < 10 && temp.x + dx[i] >= 0) && (temp.y + dy[i] >= 0 && temp.y + dy[i] < 22) && map[temp.y + dy[i]][temp.x + dx[i]] != 3 ) {
Pointer current = new Pointer(temp.y + dy[i], temp.x + dx[i], temp.steps + 1, deepCopy(temp.desk));
if(current.desk[current.y][current.x] == 4 && current.y == ty && current.x == tx) { // if the current is the target
total_paths++;
if(current.steps < step) {
final_map = deepCopy(current.desk);
step = current.steps;
}
}
else if(!hs.contains(String.valueOf(current.y) + "-" + String.valueOf(current.x))) { // the current Pointer has not been visited before, and it's not the target
if(current.desk[current.y][current.x] != 1) current.desk[current.y][current.x] = 2;
q.add(current);
hs.add(String.valueOf(current.y) + "-" + String.valueOf(current.x)); // store the coordinates of the current node, mark it as "visited"
}
}
}
}
// Printing the final matrix
System.out.println("\n===================\n");
System.out.println("Printing the final matrix");
printing(final_map);
System.out.println("\nTotal number of paths: " + total_paths);
System.out.println("Shortest one took " + step + " steps");
}
public static void main(String[] args) {
Main engine = new Main();
// randomly initializing all coordinates
Random rand = new Random();
int tx = rand.nextInt(0, 10), ty = rand.nextInt(0, 22);
int hx, hy;
while(true) {
hx = rand.nextInt(0, 10);
hy = rand.nextInt(0, 22);
if((hy != ty && hx != tx)) break;
}
int ox1, oy1, ox2, oy2, ox3, oy3;
while(true) {
ox1 = rand.nextInt(0, 10);
oy1 = rand.nextInt(0, 20);
if(oy1 != ty && oy1 == hy && ox1 != tx && ox1 != hx) break;
}
while(true) {
ox2 = rand.nextInt(0, 10);
oy2 = rand.nextInt(0, 20);
if(oy2 != ty && oy2 == hy && ox2 != tx && ox2 != hx) break;
}
while(true) {
ox3 = rand.nextInt(0, 10);
oy3 = rand.nextInt(0, 20);
if(oy3 != ty && oy3 != hy && ox3 != tx && ox3 != hx) break;
}
//engine.engine(tx, ty, hx, hy, ox1, oy1, ox2, oy2, ox3, oy3);
engine.engine(8, 1, 4, 5, 8, 2, 9, 2, 6, 1);
}
}