diff --git a/src/Fire.java b/src/Fire.java index b968c51..d9e2c7e 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,4 @@ +import java.util.*; public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a @@ -38,6 +39,49 @@ 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[][] visited = new boolean[forest.length][forest[0].length]; + return timeToBurn(forest, matchR, matchC, 0, visited); } + + + public static int timeToBurn(char[][] forest, int r, int c, int time, boolean[][] visited){ + + Queue trees = new LinkedList<>(); + trees.add(new int[] {r, c, time}); + visited[r][c] = true; + int maxTime = 0; + + while(!trees.isEmpty()){ + int[] current = trees.poll(); + System.out.println(current[2]); + int currentTime = current[2]; + + maxTime = currentTime; + for(int[] dir: directions){ + + int[] point = {current[0]+dir[0], current[1] +dir[1], currentTime +1}; + + if(point[0] >= 0 && point[0]=0 && point[1]