diff --git a/src/Fire.java b/src/Fire.java index b968c51..df261bd 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,6 @@ +import java.util.LinkedList; +import java.util.Queue; + public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a @@ -38,6 +41,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; + + + int rows = forest.length; + int cols = forest[0].length; + + boolean[][] visited = new boolean[rows][cols]; + // BFS queue + Queue queue = new LinkedList<>(); + int[][] directions = + {{1,0},//Down + {-1,0}, //Up + {0,1}, //Right + {0,-1} //Left + }; + + // Start from the lit tree with time = 0 + queue.add(new int[]{matchR, matchC, 0}); + visited[matchR][matchC] = true; + + int maxTime = 0; + + while (!queue.isEmpty()) { + int[] current = queue.poll(); + int r = current[0], c = current[1], time = current[2]; + maxTime = Math.max(maxTime, time); + + // Check all 4 directions + for (int[] d : directions) { + int newR = r + d[0]; + int newC = c + d[1]; + + + if (newR >= 0 && newR < rows && newC >= 0 && newC < cols && + !visited[newR][newC] && forest[newR][newC] == 't') { + + visited[newR][newC] = true; + queue.add(new int[]{newR, newC, time + 1}); + } + } + } + + return maxTime; + + } } \ No newline at end of file diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..1ba11fd 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,85 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testSingleTree() { + char[][] forest = { + {'t'} + }; + assertEquals(0, Fire.timeToBurn(forest, 0, 0)); + } + @Test + public void testTreeSurroundedByGround() { + char[][] forest = { + {'.','.','.'}, + {'.','t','.'}, + {'.','.','.'} + }; + assertEquals(0, Fire.timeToBurn(forest, 1, 1)); + } + + @Test + public void testNoConnectedTrees() { + char[][] forest = { + {'t','.','t'}, + {'.','.','.'}, + {'t','.','t'} + }; + assertEquals(0, Fire.timeToBurn(forest, 0, 0)); + } + + @Test + public void testLineOfTrees() { + char[][] forest = { + {'t','t','t','t','t'} + }; + assertEquals(4, Fire.timeToBurn(forest, 0, 0)); + } + + + + + @Test + public void testEmptyForest() { + char[][] forest = { + {'.','.','.'}, + {'.','.','.'}, + {'.','.','.'} + }; + assertEquals(0, Fire.timeToBurn(forest, 1, 1)); + } + + @Test + public void testFullConnectedBlock() { + char[][] forest = { + {'t','t'}, + {'t','t'} + }; + assertEquals(2, Fire.timeToBurn(forest, 0, 0)); + } + + @Test + public void testCornerUnreachableTrees() { + char[][] forest = { + {'t','.','.','t'}, + {'.','.','.','.'}, + {'t','.','.','t'} + }; + assertEquals(0, Fire.timeToBurn(forest, 0, 0)); + } + + @Test + public void testVerticalColumn() { + char[][] forest = { + {'t'}, + {'t'}, + {'t'}, + {'t'} + }; + // Fire moves down each row + assertEquals(3, Fire.timeToBurn(forest, 0, 0)); + } + + }