-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLBR.java
95 lines (80 loc) · 3.12 KB
/
LBR.java
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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class LBR {
private int cells[][];
public LBR(int[][] cells) {
this.cells = cells;
}
// struktura za vrsto celic za BFS
static class queueNode {
int pt; // ID celice (velikost matrike MxN; ID celice ∈ {1..M*N})
int dist; // dolžina poti do trenutno celice
queueNode prevPt; // vozlišče prejšnje celice
public queueNode(int pt, queueNode prevPt, int dist) {
this.pt = pt;
this.prevPt = prevPt;
this.dist = dist;
}
};
public void printPath(int from, int to) {
boolean[] visited = new boolean[cells.length*cells[0].length +1];
visited[from] = true;
// vrsta celic za BFS
Queue<queueNode> q = new LinkedList<>();
// dodaj celico from v vrsto
queueNode s = new queueNode(from, null, 0);
q.add(s);
// BFS
while (!q.isEmpty()) {
queueNode curr = q.peek();
int pt = curr.pt;
// če smo prišli do celice to => konec
if (pt == to) {
System.out.printf("Length %d:\n",q.peek().dist);
// sprehodi se nazaj po poti in točke dodajaj v sklad
queueNode node = q.remove();
Stack<Integer> route = new Stack<Integer>();
while (node.prevPt != null){
route.push(node.pt);
node = node.prevPt;
}
route.push(node.pt);
// izprazni sklad - izpiši pot
while (!route.isEmpty()) {
System.out.println(route.pop());
}
// končaj algoritem
return;
}
// sicer dequeue prvo celico in enqueue sosednje celice
queueNode prevPT = q.remove();
// v vrsto dodaj vsako veljavno sosednjo celico
for (int i = 0; i < 4; i++) {
int new_pt = pt - 1;
if (i == 1) {
// če na desnem robu preskoči
if (pt % cells[0].length == 0) continue;
new_pt = pt +1;
} else if (i == 2) {
new_pt = pt - cells[0].length;
} else if (i == 3) {
new_pt = pt + cells[0].length;
}
// če na levem robu preskoči
else if (pt % cells[0].length == 1) continue;
// če celice veljavna in še ne obiskana, jo dodaj v vrsto
int y = (new_pt-1) / cells.length;
int x = (new_pt-1) % cells.length;
if (new_pt > 0 && new_pt <= cells.length*cells[0].length
&& cells[y][x] == 0 && !visited[new_pt]) {
// označi celico za obiskano in jo dodaj v vrsto
visited[new_pt] = true;
queueNode Adjcell = new queueNode(new_pt, prevPT,curr.dist + 1);
q.add(Adjcell);
}
}
}
System.out.println("None");
}
}