From 4330c10e84de80026ade89ccdd7286264da5c1ee Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 15 May 2024 19:45:14 +0200 Subject: [PATCH] =?UTF-8?q?java:=20add=20=C2=AB2812.=20Find=20the=20Safest?= =?UTF-8?q?=20Path=20in=20a=20Grid=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- java/find-the-safest-path-in-a-grid.java | 93 ++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 java/find-the-safest-path-in-a-grid.java diff --git a/java/find-the-safest-path-in-a-grid.java b/java/find-the-safest-path-in-a-grid.java new file mode 100644 index 0000000..d92f9f6 --- /dev/null +++ b/java/find-the-safest-path-in-a-grid.java @@ -0,0 +1,93 @@ +class Solution { + private final int[][] DIRECTIONS = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + + private boolean inBounds(int[][] graph, int y, int x) { + int n = graph.length; + return y >= 0 && y < n && x >= 0 && x < n; + } + + private void setSafeness(int[][] graph, Queue queue) { + while (!queue.isEmpty()) { + int size = queue.size(); + + while (size-- > 0) { + int[] cell = queue.poll(); + int y = cell[0], x = cell[1]; + + for (int[] d : DIRECTIONS) { + int ny = y + d[0]; + int nx = x + d[1]; + + int value = graph[y][x]; + + if (inBounds(graph, ny, nx) && graph[ny][nx] == -1) { + graph[ny][nx] = value + 1; + queue.add(new int[] {ny, nx}); + } + } + } + } + } + + private int[][] toGraph(List> grid) { + int n = grid.size(); + + int[][] graph = new int[n][n]; + + Queue queue = new LinkedList<>(); + for (int i = 0; i < n * n; ++i) { + int y = i / n; + int x = i % n; + + if (grid.get(y).get(x) == 1) { + queue.add(new int[] {y, x}); + graph[y][x] = 0; + } else { + graph[y][x] = -1; + } + } + + // calculate safeness + setSafeness(graph, queue); + + return graph; + } + + private int safestPath(int[][] graph) { + int n = graph.length; + + PriorityQueue pq = new PriorityQueue<>((l, r) -> r[2] - l[2]); + + pq.add(new int[] {0, 0, graph[0][0]}); + graph[0][0] = -1; + + while (!pq.isEmpty()) { + int[] next = pq.poll(); + + int y = next[0], x = next[1], safeness = next[2]; + + // found the goal + if (y == n - 1 && x == n - 1) { + return safeness; + } + + for (int[] d : DIRECTIONS) { + int ny = y + d[0]; + int nx = x + d[1]; + + if (inBounds(graph, ny, nx) && graph[ny][nx] != -1) { + pq.add(new int[] {ny, nx, Math.min(safeness, graph[ny][nx])}); + graph[ny][nx] = -1; + } + } + } + + return -1; + } + + public int maximumSafenessFactor(List> grid) { + int n = grid.size(); + int[][] graph = toGraph(grid); + return safestPath(graph); + } +}