diff --git a/src/Fire.java b/src/Fire.java index b968c51..52a9dee 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,9 @@ +import java.util.LinkedList; +import java.util.Queue; +import java.util.ArrayList; +import java.util.List; + + public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a @@ -38,6 +44,47 @@ public class Fire { public static int timeToBurn(char[][] forest, int matchR, int matchC) { // HINT: when adding to your BFS queue, you can include more information than // just a location. What other information might be useful? - return -1; + boolean[][] onFire = new boolean[forest.length][forest[0].length]; + Queue queue = new LinkedList<>(); + int maxDepth = 0; + queue.add(new int[]{matchR, matchC, maxDepth}); + + while (!queue.isEmpty()) { + int[] current = queue.poll(); + int currR = current[0]; + int currC = current[1]; + int depth = current[2] + 1; + + if(onFire[currR][currC]) { + continue; + } + + onFire[currR][currC] = true; + + List moves = possibleMoves(forest, current); + + for(int[] move : moves) { + queue.add(new int[]{move[0], move[1], depth}); + + } + maxDepth = Math.max(maxDepth, depth); + } + return maxDepth-1; + } + public static List possibleMoves(char[][] forest, int[] curr) { + int[][] directions = {{-1,0},{1,0},{0,-1},{0,1}}; + List moves = new ArrayList<>(); + for (int[] dir : directions) { + int newROW = curr[0] + dir[0]; + int newCOL = curr[1] + dir[1]; + if (newROW>=0&& + newROW=0&& + newCOL