diff --git a/src/Fire.java b/src/Fire.java index b968c51..75313e9 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,8 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +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 +43,61 @@ 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[] start = {matchR, matchC, 0}; + Queue lit = new LinkedList<>(); + lit.add(start); + + boolean[][] visited = new boolean[forest.length][forest[0].length]; + int maxTime = 0; + + while (!lit.isEmpty()){ + int[] current = lit.poll(); + int currentTime = current[2]; + + if (currentTime > maxTime){ + maxTime = currentTime; + } + + List nextMoves = traverseFire(forest, current, currentTime); + + for (int[] move : nextMoves) { + int newR = move[0]; + int newC = move[1]; + + if (!visited[newR][newC]) { + visited[newR][newC] = true; + lit.add(new int[]{newR, newC, currentTime + 1}); + } + } + + } + + return maxTime; + } + + public static List traverseFire(char[][] forest, int[] current, int time){ + List moves = new ArrayList<>(); + int[][] steps = { + {-1, 0}, + {1, 0}, + {0, -1}, + {0, 1} + }; + + int curR = current[0]; + int curC = current[1]; + + for (int[] step : steps) { + int newR = curR + step[0]; + int newC = curC + step[1]; + + if (newR >= 0 && newR < forest.length && + newC >= 0 && newC < forest[0].length && + forest[newR][newC] == 't') { + moves.add(new int[]{newR, newC, time}); + } + } + + return moves; } } \ No newline at end of file diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..740404f 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,89 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testTimeToBurnSimpler() { + char[][] forest = { + {'t','.','.','t'}, + {'.','.','t','t'}, + {'t','.','t','t'}, + {'t','t','t','t'} + }; + + int matchR = 2; + int matchC = 2; + + int expected = 4; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurnEmptySurroundedTree() { + char[][] forest = { + {'.','.','.','.'}, + {'.','.','.','t'}, + {'.','.','.','.'}, + {'.','.','.','.'} + }; + + int matchR = 1; + int matchC = 3; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurnOneTree() { + char[][] forest = { + {'t'} + }; + + int matchR = 0; + int matchC = 0; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurnDiagonalTrees() { + char[][] forest = { + {'t','.','t'}, + {'.','t','.'}, + {'t','.','t'} + }; + + int matchR = 1; + int matchC = 1; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurnAllTrees() { + char[][] forest = { + {'t','t','t'}, + {'t','t','t'}, + {'t','t','t'} + }; + + int matchR = 1; + int matchC = 1; + + int expected = 2; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } }