-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithms.java
More file actions
65 lines (61 loc) · 1.72 KB
/
Algorithms.java
File metadata and controls
65 lines (61 loc) · 1.72 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
/**
*
* @author Zafrir
*/
public class Algorithms {
protected String path;
protected int numOfVertexExp;
protected int cost;
protected double startTime;
protected char[][] mat;
protected Node start;
protected Node finish;
public String makePath(Node start) {
path="";
cost=0;
Node parent = start.getParent();
int x, y;
while (parent != null) {
x = start.getX() - parent.getX();
y = start.getY() - parent.getY();
switch (mat[start.getY()][start.getX()]) {
case 'D':
cost += 2;
break;
case 'R':
cost += 3;
break;
case 'H':
cost += 5;
break;
case 'G':
cost += 5;
break;
}
if (y > 0) {
if (x > 0) {
path = "RD-" + path;
} else if (x == 0) {
path = "D-" + path;
} else {
path = "DL-" + path;
}
} else if (y < 0) {
if (x > 0) {
path = "RU-" + path;
} else if (x == 0) {
path = "U-" + path;
} else {
path = "UL-" + path;
}
} else if (x > 0) {
path = "R-" + path;
} else {
path = "L-" + path;
}
start = parent;
parent = parent.getParent();
}
return path;
}
}