-
Notifications
You must be signed in to change notification settings - Fork 4
/
SearchWay.java
57 lines (52 loc) · 1.76 KB
/
SearchWay.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
public class SearchWay {
public static String SearchWays(int[][] arr) {
StringBuilder str = new StringBuilder();
int x = 0;
int y = 0;
int max = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] == -2) {
x = i;
y = j;
str.append(y + ":" + x + ",");
}
if (arr[i][j] > max) {
max = arr[i][j];
}
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
while (max > 0) {
if (x > 0 && y > 0) {
if (arr[x + 1][y] == max) {
x = x + 1;
str.append(y + ":" + x + ",");
}
}
if (x > 0 && y > 0) {
if (arr[x - 1][y] == max) {
x = x - 1;
str.append(y + ":" + x + ",");
}
}
if (x > 0 && y > 0) {
if (arr[x][y + 1] == max) {
y = y + 1;
str.append(y + ":" + x + ",");
}
}
if (x > 0 && y > 0) {
if (arr[x][y - 1] == max) {
y = y - 1;
str.append(y + ":" + x + ",");
}
}
max--;
}
}
}
return str.deleteCharAt(str.length() - 1).toString();
}
}