-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrienteering.java
More file actions
189 lines (166 loc) · 6.14 KB
/
Orienteering.java
File metadata and controls
189 lines (166 loc) · 6.14 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package MadTools;
import java.util.ArrayList;
import java.util.Collections;
/**
* Simple orienteering tool.
* The board is a character array, where X is a blocked space, * is source, and # is destination.
* Multiple destinations can exist, but only distance to the closest one is returned.
* Multiple sources can exist, but only the first one will be fed to the algorithm.
* Returns distance if path is found, or -1 if not found.
* @author Mad Scientist
*/
public class Orienteering
{
/**
* Stores list of visited locations.
*/
private static ArrayList<Integer[]> seen;
/**
* Board.
*/
private char[][] b;
/**
* Maximum search depth
*/
private int m;
/**
* Board Dimensions
*/
private int l,w;
/**
* Accepts a board and returns the distance from source to destination if it exists, or -1 if it doesn't.
* @param b
* @return
*/
public int findDist(char[][] b)
{
this.b = b;
l=b.length; w=b[0].length; m=l*w;
int dist;
seen = new ArrayList<>();
for(int i=0; i < l; i++) for(int j=0; j<w; j++)
if(b[i][j]=='*') return ((dist=DFS(i,j,0))>m) ? -1 : dist-1;
return -1;
}
/**
* Recursively finds path using DFS.
* Returns some value greater than max if path is not found, or if max recursion depth is reached.
* @param grid
* @param sourceX
* @param sourceY
* @param x
* @param y
* @param d
* @return
*/
private int DFS(int x, int y, int d)
{
visit(x,y,d);
if(b[x][y]=='#') return 1;
if(b[x][y]=='X' || d>m) return m+1;
ArrayList<Integer> r = new ArrayList<>();
if(!((x==0) || isSeen(x-1,y,d+1) || (b[x-1][y]=='X'))) r.add(DFS(x-1,y,d+1));
if(!((x==l-1) || isSeen(x+1,y,d+1) || (b[x+1][y]=='X'))) r.add(DFS(x+1,y,d+1));
if(!((y==0) || isSeen(x,y-1,d+1) || (b[x][y-1]=='X'))) r.add(DFS(x,y-1,d+1));
if(!((y==w-1) || isSeen(x,y+1,d+1) || (b[x][y+1]=='X'))) r.add(DFS(x,y+1,d+1));
Collections.sort(r);
return r.isEmpty() ? m+1 : r.get(0)+1;
}
/**
* Returns true if point has been visited at a shallower or equal depth.
* @param x
* @param y
* @param d
* @return
*/
private boolean isSeen(int x, int y, int d)
{
return seen.stream().anyMatch((pt) -> (pt[0]==x && pt[1]==y && pt[2]<=d));
}
/**
* Adds point to visited if unseen, or updates depth if seen.
* @param x
* @param y
* @param d
*/
void visit(int x, int y, int d)
{
for(Integer[] pt : seen)
if(pt[0]==x && pt[1]==y)
{
pt[2]=d;
return;
}
Integer[] newPt = {x,y,d};
seen.add(newPt);
}
/**
* Examples.
* @param args
*/
public static void main(String args[])
{
char[][] b1 = {
{'*','#'}
};
char[][] b2 = {
{'X','X','X','X','X'},
{'X','*','X',' ','X'},
{'X',' ','X','#','X'},
{'X','X','X','X','X'}
};
char[][] b3 = {
{'X','X','X','X','X','X'},
{'X','*',' ','X',' ','X'},
{'X',' ','X',' ',' ','X'},
{'X',' ','X',' ','X','X'},
{'X',' ',' ',' ',' ','X'},
{'X',' ',' ',' ',' ','X'},
{'X','X',' ','X',' ','X'},
{'X',' ',' ','X',' ','X'},
{'X',' ','X',' ','#','X'},
{'X','X','X','X','X','X'}
};
char[][] b4 = {
{'*',' ','X',' ',' ',' ','X',' ','X',' ',' ',' ','X',' ','X',' ','#'},
{'X',' ','X',' ','X',' ',' ',' ','X',' ','X',' ',' ',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ',' '},
{' ',' ','X',' ','X','X','X',' ','X',' ','X','X','X',' ','X','X',' '},
{' ','X','X',' ',' ',' ','X',' ','X',' ',' ',' ','X',' ','X',' ',' '},
{' ',' ','X','X','X',' ','X',' ','X','X','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
{'X',' ',' ',' ','X',' ','X',' ',' ',' ','X',' ','X',' ',' ',' ','X'},
{'X','X',' ',' ',' ',' ','X','X',' ',' ',' ',' ','X','X',' ','X','X'}
};
Orienteering o = new Orienteering();
System.out.println(o.findDist(b1));
System.out.println(o.findDist(b2));
System.out.println(o.findDist(b3));
System.out.println(o.findDist(b4));
}
}