-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCastleOnTheGrid.java
More file actions
133 lines (114 loc) · 4.11 KB
/
CastleOnTheGrid.java
File metadata and controls
133 lines (114 loc) · 4.11 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
/*java 15
source of this code https://github.com/Mukit09/HackerRank-Solutions/blob/master/DataStructure/CastleOnTheGrid/Solution.java
*/
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
private static int[] dx = {0, -1, 0, 1};
private static int[] dy = {-1, 0, 1, 0};
static class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
private static boolean checkedPoint(String[] grid, int x, int y, int n) {
if(x < 0 || x >= n || y < 0 || y >= n)
return false;
if(grid[x].charAt(y) == 'X')
return false;
return true;
}
private static int bfs(String[] grid, int startX, int startY, int goalX, int goalY) {
int n = grid.length;
boolean visit[][] = new boolean[n][n];
int cost[][] = new int[n][n];
Queue<Point> queue = new LinkedList<>();
Point point = new Point(startX, startY);
queue.offer(point);
while(!queue.isEmpty()) {
Point point1 = queue.poll();
int x = point1.getX();
int y = point1.getY();
for(int i = 0; i<4; i++) {
int tempX = x;
int tempY = y;
while(true) {
int newX = tempX + dx[i];
int newY = tempY + dy[i];
boolean isValid = checkedPoint(grid, newX, newY, n);
if(!isValid)
break;
if (!visit[newX][newY]) {
cost[newX][newY] = cost[x][y] + 1;
if (newX == goalX && newY == goalY)
return cost[newX][newY];
visit[newX][newY] = true;
queue.offer(new Point(newX, newY));
}
tempX = newX;
tempY = newY;
}
}
}
return -1;
}
/*
* Complete the 'minimumMoves' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. STRING_ARRAY grid
* 2. INTEGER startX
* 3. INTEGER startY
* 4. INTEGER goalX
* 5. INTEGER goalY
*/
public static int minimumMoves(List<String> grid, int startX, int startY, int goalX, int goalY) {
// Write your code here
String[] stringArray = grid.toArray(new String[0]);
int minMoves = bfs(stringArray, startX, startY, goalX, goalY);
return minMoves;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<String> grid = IntStream.range(0, n).mapToObj(i -> {
try {
return bufferedReader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.collect(toList());
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int startX = Integer.parseInt(firstMultipleInput[0]);
int startY = Integer.parseInt(firstMultipleInput[1]);
int goalX = Integer.parseInt(firstMultipleInput[2]);
int goalY = Integer.parseInt(firstMultipleInput[3]);
int result = Result.minimumMoves(grid, startX, startY, goalX, goalY);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}