-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCastle on the Grid.java
143 lines (106 loc) · 3.71 KB
/
Castle on the Grid.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
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
/*
Problem Title: Castle on the Grid
Problem URL: https://www.hackerrank.com/challenges/one-month-preparation-kit-castle-on-the-grid/problem?h_l=interview&playlist_slugs%5B%5D=preparation-kits&playlist_slugs%5B%5D=one-month-preparation-kit&playlist_slugs%5B%5D=one-month-week-four
Max Score: 100
Score: 100
Language: Java
Category: One Month Preparation
*/
class Result {
public static int minimumMoves(List<String> grid, int startX, int startY, int goalX, int goalY) {
int n = grid.size();
XY start = new XY(startX, startY);
XY end = new XY(goalX, goalY);
Set<XY> visited = new HashSet<XY>();
char[][] mat = new char[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
mat[i][j] = grid.get(i).charAt(j);
return minSteps(mat, start, end, visited);
}
private static int minSteps(char[][] grid, XY curr, XY end, Set<XY> visited) {
Queue<XY> queue = new LinkedList<XY>();
queue.add(curr);
visited.add(curr);
Queue<Integer> step = new LinkedList<Integer>();
step.add(0);
while (!queue.isEmpty()) {
XY nextXY = queue.remove();
int hop = step.remove();
if (nextXY.equals(end)) return hop;
List<XY> next = getNext(grid, nextXY, visited);
visited.addAll(next);
queue.addAll(next);
for (int i = 0; i < next.size(); i++) {
step.add(hop + 1);
}
}
return Integer.MAX_VALUE;
}
private static List<XY> getNext(char[][] grid, XY curr, Set<XY> visited) {
List<XY> next = new ArrayList<XY>();
if (curr.dir == null || curr.dir == Dir.Y) {
int x = curr.x;
for (int i = x - 1; i >= 0; i--) {
if (grid[i][curr.y] == 'X') break;
final XY xy = new XY(i, curr.y, Dir.X);
if (!visited.contains(xy)) next.add(xy);
}
for (int i = x + 1; i < grid.length; i++) {
if (grid[i][curr.y] == 'X') break;
final XY xy = new XY(i, curr.y, Dir.X);
if (!visited.contains(xy)) next.add(xy);
}
}
if (curr.dir == null || curr.dir == Dir.X) {
int y = curr.y;
for (int i = y - 1; i >= 0; i--) {
if (grid[curr.x][i] == 'X') break;
final XY xy = new XY(curr.x, i, Dir.Y);
if (!visited.contains(xy)) next.add(xy);
}
for (int i = y + 1; i < grid.length; i++) {
if (grid[curr.x][i] == 'X') break;
final XY xy = new XY(curr.x, i, Dir.Y);
if (!visited.contains(xy)) next.add(xy);
}
}
return next;
}
private static class XY {
int x, y;
Dir dir;
public XY(int x, int y) {
this.x = x;
this.y = y;
dir = null;
}
public XY(int x, int y, Dir dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
XY xy = (XY) o;
return x == xy.x && y == xy.y;
}
@Override
public int hashCode() {
return 31 * x + y;
}
@Override
public String toString() {
return "XY{" +
"x=" + x +
", y=" + y +
", dir=" + dir +
'}';
}
}
private enum Dir {
X, Y;
}
}