diff --git a/src/Fire.java b/src/Fire.java index b968c51..e5f9d53 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,85 @@ 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; + + //BFS + //int[] = [r ,c, time] + //return time + + //checking if match lit is a tree, if no tree return 0 else do everything that follows + if(forest[matchR][matchC] == '.') + { + return 0; + } + + boolean[][] visited = new boolean[forest.length][forest[0].length]; + int[] initialBurnTime = {matchR, matchC, 0}; + + int[][] directions = { + {-1,0}, //up + {1,0}, //down + {0,-1}, //left + {0,1} //right + }; + + int burnTime = 0; + + Queue q = new LinkedList<>(); + q.add(initialBurnTime); + int[] currBurningTree = new int[3]; + while(!q.isEmpty()) + { + currBurningTree = q.poll(); + if(visited[currBurningTree[0]][currBurningTree[1]]) + { + continue; + } + + visited[currBurningTree[0]][currBurningTree[1]] = true; + + if(forest[currBurningTree[0]][currBurningTree[1]] == 't') + { + burnTime = Math.max(burnTime, currBurningTree[2]); + } + + //find other burning trees method call here + List treeNeighbors = getTreeNeighbors(currBurningTree[0], currBurningTree[1], directions, forest, currBurningTree[2]); + q.addAll(treeNeighbors); + } + return burnTime; } + + //MORE HINT: current depth for third variable + //keep track of the depth for each movement + //return last depth element in the q for the burn time + + + public static List getTreeNeighbors(int r, int c, int[][] directions, char[][] forest, int currTime) + { + List possibleTrees = new ArrayList<>(); + + for(int[] d : directions) + { + int rr = r + d[0]; + int cc = c + d[1]; + + + if (rr >= 0 && cc >= 0 && rr < forest.length + && cc < forest[0].length && forest[rr][cc] == 't') { + + possibleTrees.add(new int[]{rr, cc, currTime + 1}); + + }//end if + }//end for + + return possibleTrees; + }//end getTreeNeighbors + + + //check if in bounds + //check base case (epmty space) + //loop over directions and as long as trees contiue to burn increase time + + //{current row, current column, current time}, put time in and take time back out + } \ No newline at end of file diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..6a7dae3 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -1,4 +1,5 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -20,4 +21,92 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } -} + + @Test + public void testSingleTree() { + char[][] forest = { + {'.','.','.'}, + {'.','t','.'}, + {'.','.','.'} + }; + + int matchR = 1; + int matchC = 1; + + int expected = 0; // burns immediately + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + }//end testSingleTree + + @Test + public void testFarAwayTrees() { + char[][] forest = { + {'t','.','t'}, + {'.','.','.'}, + {'t','.','t'} + }; + + int matchR = 0; + int matchC = 0; + + int expected = 0; //trees are too far to be burned + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + }//end testFarAwayTrees + + @Test + public void testTreesConnected() { + 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); + }//end testTreesConnected + + @Test + public void testOutOfBounds() { + char[][] forest = { + {'t','t','t'}, + {'t','t','t'}, + {'t','t','t'} + }; + + //out of bounds + int matchR = -1; + int matchC = 5; + + assertThrows(ArrayIndexOutOfBoundsException.class, () -> { + Fire.timeToBurn(forest, matchR, matchC); + }); + }//end testOutOfBounds + + @Test + public void testNoFire() { + char[][] forest = { + {'t', 't', 't'}, + {'t', '.', 't'}, + {'t', 't', 't'} + }; + + int matchR = 1; + int matchC = 1; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + }//end testNoFire + + + +}//end file