From e86da1a66ad160ff0226633dd2823bb538a50f43 Mon Sep 17 00:00:00 2001 From: X BELL <194058603+bellxalli@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:22:25 -0700 Subject: [PATCH 1/6] added BFS logic to timeToBurn and created helper for finding possible burn trees --- src/Fire.java | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index b968c51..0d95d99 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,69 @@ 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 + boolean[][] visited = new boolean[forest.length][forest[0].length]; + int[] burnTime = {matchR, matchC, 0}; + + int[][] directions = { + {-1,0}, //up + {1,0}, //down + {0,-1}, //left + {0,1} //right + }; + + Queue q = new LinkedList<>(); + q.add(burnTime); + 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') + { + currBurningTree[2] += 1; + } + + //find other burning trees method call here + List treeNeighbors = getTreeNeighbors(currBurningTree[0], currBurningTree[1], directions, forest); + q.addAll(treeNeighbors); + } + return currBurningTree[2]; } + + + + public static List getTreeNeighbors(int r, int c, int[][]directions, char[][]forest) + { + 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[rr].length + && forest[rr][cc] == '.') + { + int[] validTrees = {r ,c}; + possibleTrees.add(validTrees); + } + } + + return possibleTrees; + } + //check if in bounds + //check base case (epmty space) + //loop over directions and as long as trees contiue to burn increase time + } \ No newline at end of file From 1adf9aed0a484d4f34f6323b881471a5f6cafe4b Mon Sep 17 00:00:00 2001 From: X BELL <194058603+bellxalli@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:30:23 -0700 Subject: [PATCH 2/6] fixed logic error with passing arrays --- src/Fire.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 0d95d99..1cf2d96 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -44,7 +44,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { //int[] = [r ,c, time] //return time boolean[][] visited = new boolean[forest.length][forest[0].length]; - int[] burnTime = {matchR, matchC, 0}; + int[] initialBurnTime = {matchR, matchC, 0}; int[][] directions = { {-1,0}, //up @@ -53,8 +53,10 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { {0,1} //right }; + int burnTime = 0; + Queue q = new LinkedList<>(); - q.add(burnTime); + q.add(initialBurnTime); int[] currBurningTree = new int[3]; while(!q.isEmpty()) { @@ -68,14 +70,14 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { if(forest[currBurningTree[0]][currBurningTree[1]] == 't') { - currBurningTree[2] += 1; + burnTime += currBurningTree[2]; } //find other burning trees method call here List treeNeighbors = getTreeNeighbors(currBurningTree[0], currBurningTree[1], directions, forest); q.addAll(treeNeighbors); } - return currBurningTree[2]; + return burnTime; } @@ -93,7 +95,7 @@ public static List getTreeNeighbors(int r, int c, int[][]directions, char if(rr >= 0 && cc >= 0 && rr < forest.length && cc < forest[rr].length && forest[rr][cc] == '.') { - int[] validTrees = {r ,c}; + int[] validTrees = {r , c, 1}; possibleTrees.add(validTrees); } } From 8caa3cc2d06dbb780aa64d5812cf65935121e531 Mon Sep 17 00:00:00 2001 From: X BELL <194058603+bellxalli@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:41:57 -0700 Subject: [PATCH 3/6] added comments for possible fixes --- src/Fire.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index 1cf2d96..8f67afb 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -80,6 +80,9 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { 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) From 200c5738748144d0822162e3cf855631f748ab09 Mon Sep 17 00:00:00 2001 From: EmilyM <155667566+EmilyMenken@user.noreply.github.com> Date: Thu, 5 Jun 2025 11:42:51 -0700 Subject: [PATCH 4/6] added comments to help with implementation --- src/Fire.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index 1cf2d96..60f3c7d 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -106,4 +106,6 @@ public static List getTreeNeighbors(int r, int c, int[][]directions, char //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 From 839b094f89d8e83ed75e9c42a26110c97fdedb42 Mon Sep 17 00:00:00 2001 From: EmilyM <155667566+EmilyMenken@user.noreply.github.com> Date: Mon, 9 Jun 2025 12:31:10 -0700 Subject: [PATCH 5/6] changed some logic in getTreeNeighbors and timeToBurn + added 5 tests, last test failing atm --- src/Fire.java | 24 +++++++------ src/FireTest.java | 91 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 12 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 5e832bc..69b8c01 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -70,11 +70,11 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { if(forest[currBurningTree[0]][currBurningTree[1]] == 't') { - burnTime += currBurningTree[2]; + burnTime = Math.max(burnTime, currBurningTree[2]); } //find other burning trees method call here - List treeNeighbors = getTreeNeighbors(currBurningTree[0], currBurningTree[1], directions, forest); + List treeNeighbors = getTreeNeighbors(currBurningTree[0], currBurningTree[1], directions, forest, currBurningTree[2]); q.addAll(treeNeighbors); } return burnTime; @@ -85,7 +85,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { //return last depth element in the q for the burn time - public static List getTreeNeighbors(int r, int c, int[][]directions, char[][]forest) + public static List getTreeNeighbors(int r, int c, int[][] directions, char[][] forest, int currTime) { List possibleTrees = new ArrayList<>(); @@ -95,16 +95,18 @@ public static List getTreeNeighbors(int r, int c, int[][]directions, char int cc = c + d[1]; - if(rr >= 0 && cc >= 0 && rr < forest.length && cc < forest[rr].length - && forest[rr][cc] == '.') - { - int[] validTrees = {r , c, 1}; - possibleTrees.add(validTrees); - } - } + 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 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 From 7a4c9b144b04e4173899b8632565c3b0517659b1 Mon Sep 17 00:00:00 2001 From: X BELL <194058603+bellxalli@users.noreply.github.com> Date: Mon, 9 Jun 2025 22:45:01 -0700 Subject: [PATCH 6/6] added if statement to fix last test case failure --- src/Fire.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index 69b8c01..e5f9d53 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -43,6 +43,13 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { //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};