From 25441d494617c9b77f00df670368ce560b4ac2b0 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 5 Jun 2025 10:53:39 -0700 Subject: [PATCH 1/9] need to test --- src/Fire.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..baaaaf3 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,10 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +import javax.management.Query; + public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a @@ -38,6 +45,33 @@ 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? + boolean[][] visited = new boolean[forest.length][forest[0].length]; + char[] unburn = forest[0]; + + Queue queue = new LinkedList<>(); + queue.add(unburn); + + int unburnStarter = 0; + + while (!queue.isEmpty()) { + char[] current = queue.poll(); + char curR = current[0]; + char curC = current[1]; + + if(visited[curR][curC]){ + continue; + } + + if(forest[curR][curC] == 't'){ + unburnStarter++; + } + + queue.addAll(getNeighbors(forest, current)); + } + + return -1; } + + } \ No newline at end of file From fb370180acd4c9c59dbd8703291a0b2f1ecb08ed Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 5 Jun 2025 10:55:30 -0700 Subject: [PATCH 2/9] fix return --- src/Fire.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index baaaaf3..b15758e 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -70,7 +70,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { } - return -1; + return unburnStarter; } From 573eef01bd936c208a4eb398023dafefb18ea366 Mon Sep 17 00:00:00 2001 From: Squizard <184549450+BradyLeg@users.noreply.github.com> Date: Thu, 5 Jun 2025 10:56:57 -0700 Subject: [PATCH 3/9] Created neighborTrees method --- src/Fire.java | 55 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index b968c51..74bbc4c 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,17 +1,23 @@ +import java.util.ArrayList; +import java.util.List; + public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a * match is lit at a given location. * - * The forest is represented via a rectangular 2d char array where t represents a tree + * The forest is represented via a rectangular 2d char array where t represents + * a tree * and . represents an empty space. * - * At time 0, the tree at location [matchR, matchC] is set on fire. At every subsequent - * time step, any tree that is adjacent (up/down/left/right) to a burning tree is also - * set on fire. + * At time 0, the tree at location [matchR, matchC] is set on fire. At every + * subsequent + * time step, any tree that is adjacent (up/down/left/right) to a burning tree + * is also + * set on fire. * * - * EXAMPLE + * EXAMPLE * forest: * * t..tttt.t @@ -25,12 +31,16 @@ public class Fire { * Result: 8 * * Explanation: - * At time 0, the tree at (2, 6) is set on fire. At time 1, its adjacent trees also catch on fire - * At time 2, the trees adjacent to those catch as well. At time 8, the last tree to catch is at - * (0,6). In this example, there is one tree that never burns, so it is not included in the time calculation. + * At time 0, the tree at (2, 6) is set on fire. At time 1, its adjacent trees + * also catch on fire + * At time 2, the trees adjacent to those catch as well. At time 8, the last + * tree to catch is at + * (0,6). In this example, there is one tree that never burns, so it is not + * included in the time calculation. * * - * @param forest a 2d array where t represents a tree and . represents the ground + * @param forest a 2d array where t represents a tree and . represents the + * ground * @param matchR The row the match is lit at * @param matchC The column the match is lit at * @return the time at which the final tree to be incinerated starts burning @@ -40,4 +50,31 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { // just a location. What other information might be useful? return -1; } + + public static List neighborTrees(char[][] forest, int[] location) { + int curR = location[0]; + int curC = location[1]; + List neighbors = new ArrayList<>(); + + int[][] directions = { + { -1, 0 }, + { 1, 0 }, + { 0, -1 }, + { 0, 1 } + }; + + for (int[] move : directions) { + int newR = curR + move[0]; + int newC = curC + move[1]; + + if (newR >= 0 && newR < forest.length && + newC >= 0 && newC < forest[newR].length && + forest[newR][newC] == 't') { + int[] validMove = { newR, newC }; + neighbors.add(validMove); + } + } + + return neighbors; + } } \ No newline at end of file From cfa63b42b235bd388fff4f1104844866100c6df4 Mon Sep 17 00:00:00 2001 From: Squizard <184549450+BradyLeg@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:15:31 -0700 Subject: [PATCH 4/9] Added depth to neighborTrees --- src/Fire.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 90ed24c..bf90608 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -87,6 +87,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { public static List neighborTrees(char[][] forest, int[] location) { int curR = location[0]; int curC = location[1]; + int depth = location[2] + 1; List neighbors = new ArrayList<>(); int[][] directions = { @@ -103,7 +104,7 @@ public static List neighborTrees(char[][] forest, int[] location) { if (newR >= 0 && newR < forest.length && newC >= 0 && newC < forest[newR].length && forest[newR][newC] == 't') { - int[] validMove = { newR, newC }; + int[] validMove = { newR, newC, depth }; neighbors.add(validMove); } } From ea397cbbff743c69b2b290c8a721acd81b65b966 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:15:44 -0700 Subject: [PATCH 5/9] slight change --- src/Fire.java | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 90ed24c..5ce4cb4 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -59,28 +59,24 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { boolean[][] visited = new boolean[forest.length][forest[0].length]; char[] unburn = forest[matchR]; - Queue queue = new LinkedList<>(); - queue.add(unburn); + Queue queue = new LinkedList<>(); int unburnStarter = 0; + int[] match = {matchR, matchC, 0}; + queue.add(match); while (!queue.isEmpty()) { - char[] current = queue.poll(); - char curR = current[0]; - char curC = current[1]; + int[] current = queue.poll(); + int curR = current[0]; + int curC = current[1]; if(visited[curR][curC]){ continue; } - if(forest[curR][curC] == 't'){ - unburnStarter++; - } - - queue.addAll(neighborTrees(forest, current)); + queue.addAll(neighborTrees(forest, match)); } - return unburnStarter; } @@ -101,9 +97,10 @@ public static List neighborTrees(char[][] forest, int[] location) { int newC = curC + move[1]; if (newR >= 0 && newR < forest.length && - newC >= 0 && newC < forest[newR].length && - forest[newR][newC] == 't') { + newC >= 0 && newC < forest[newR].length && + forest[newR][newC] == 't') { int[] validMove = { newR, newC }; + neighbors.add(validMove); } } From a5a403974cbdc3698226bfacf2de3a5ec1951f18 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:22:28 -0700 Subject: [PATCH 6/9] stack overflow issues --- src/Fire.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 84dfc35..696751c 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,13 +1,9 @@ import java.util.ArrayList; -<<<<<<< HEAD -import java.util.List; -======= import java.util.LinkedList; import java.util.List; import java.util.Queue; import javax.management.Query; ->>>>>>> fb370180acd4c9c59dbd8703291a0b2f1ecb08ed public class Fire { /** @@ -57,7 +53,6 @@ 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? boolean[][] visited = new boolean[forest.length][forest[0].length]; - char[] unburn = forest[matchR]; Queue queue = new LinkedList<>(); @@ -75,6 +70,8 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { } queue.addAll(neighborTrees(forest, match)); + + unburnStarter = current[2]; } return unburnStarter; From bda9c3676b10e772e27c849a85176d7869a721e5 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:27:13 -0700 Subject: [PATCH 7/9] fix infinity --- src/Fire.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 696751c..645c077 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -69,7 +69,9 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { continue; } - queue.addAll(neighborTrees(forest, match)); + visited[curR][curC] = true; + + queue.addAll(neighborTrees(forest, current)); unburnStarter = current[2]; } From 57eafeb59f0c4e111098b12956e29e7349a7302a Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:42:16 -0700 Subject: [PATCH 8/9] added one test --- src/Fire.java | 10 +++++++++- src/FireTest.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 645c077..b393abf 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -52,7 +52,15 @@ 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? - boolean[][] visited = new boolean[forest.length][forest[0].length]; + + int maxCol = 0; + for(char[] row: forest){ + if(maxCol < row.length){ + maxCol = row.length; + } + } + + boolean[][] visited = new boolean[forest.length][maxCol]; Queue queue = new LinkedList<>(); diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..0079d0e 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,22 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void outboundJaggedCol() { + char[][] forest = { + {'t','.','.','t','t','t','t','.'}, + {'.','.','t','t','.','.','.','.','t'}, + {'.','.','t','t','t','t','t','t','t'}, + {'t','t','t','t'} + }; + + int matchR = 2; + int matchC = 6; + + int expected = 8; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } } From 2eb4922c3d5ab17590f961f03e202adb8008cb42 Mon Sep 17 00:00:00 2001 From: Squizard <184549450+BradyLeg@users.noreply.github.com> Date: Thu, 5 Jun 2025 12:02:35 -0700 Subject: [PATCH 9/9] Added tests for neighborTrees method --- src/FireTest.java | 88 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 4 deletions(-) diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..2b6f3e1 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -1,15 +1,19 @@ +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.List; + import org.junit.jupiter.api.Test; public class FireTest { @Test public void testTimeToBurnExample() { char[][] forest = { - {'t','.','.','t','t','t','t','.','t'}, - {'.','.','t','t','.','.','.','.','t'}, - {'.','.','t','t','t','t','t','t','t'}, - {'t','t','t','t','.','.','.','.','.'} + { 't', '.', '.', 't', 't', 't', 't', '.', 't' }, + { '.', '.', 't', 't', '.', '.', '.', '.', 't' }, + { '.', '.', 't', 't', 't', 't', 't', 't', 't' }, + { 't', 't', 't', 't', '.', '.', '.', '.', '.' } }; int matchR = 2; @@ -20,4 +24,80 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testNeighborTrees_bottomRightCornerBlocked() { + char[][] forest = { + { 't', '.', '.', 't', 't', 't', 't', '.', 't' }, + { '.', '.', 't', 't', '.', '.', '.', '.', 't' }, + { '.', '.', 't', 't', 't', 't', 't', 't', '.' }, + { 't', 't', 't', 't', '.', '.', '.', '.', 't' } + }; + + int[] location = { 3, 8, 0 }; + List result = Fire.neighborTrees(forest, location); + + assertEquals(0, result.size()); + + } + + @Test + public void testNeighborTrees_bottomRightCornerUpOnly() { + char[][] forest = { + { 't', '.', '.', 't', 't', 't', 't', '.', 't' }, + { '.', '.', 't', 't', '.', '.', '.', '.', 't' }, + { '.', '.', 't', 't', 't', 't', 't', 't', 't' }, + { 't', 't', 't', 't', '.', '.', '.', '.', 't' } + }; + + int[] location = { 3, 8, 2 }; + List result = Fire.neighborTrees(forest, location); + + assertEquals(1, result.size()); + assertEquals(2, result.get(0)[0]); + assertEquals(8, result.get(0)[1]); + assertEquals(3, result.get(0)[2]); + } + + @Test + public void testNeighborTrees_middleAllPossible() { + char[][] forest = { + { 't', '.', '.', 't', 't', 't', 't', '.', 't' }, + { '.', '.', 't', 't', 't', '.', '.', '.', 't' }, + { '.', '.', 't', 't', 't', 't', 't', 't', 't' }, + { 't', 't', 't', 't', 't', '.', '.', '.', 't' } + }; + + int[] location = { 2, 4, 4 }; + List result = Fire.neighborTrees(forest, location); + + assertEquals(4, result.size()); + assertEquals(1, result.get(0)[0]); + assertEquals(4, result.get(0)[1]); + assertEquals(5, result.get(0)[2]); + assertEquals(3, result.get(1)[0]); + assertEquals(4, result.get(1)[1]); + assertEquals(5, result.get(1)[2]); + assertEquals(2, result.get(2)[0]); + assertEquals(3, result.get(2)[1]); + assertEquals(5, result.get(2)[2]); + assertEquals(2, result.get(3)[0]); + assertEquals(5, result.get(3)[1]); + assertEquals(5, result.get(3)[2]); + } + + @Test + public void testNeighborTrees_middleNonePossible() { + char[][] forest = { + { 't', '.', '.', 't', 't', 't', 't', '.', 't' }, + { '.', '.', 't', '.', '.', '.', '.', '.', 't' }, + { '.', '.', 't', '.', 't', '.', 't', 't', 't' }, + { 't', 't', 't', 't', '.', '.', '.', '.', 't' } + }; + + int[] location = { 2, 4, 4 }; + List result = Fire.neighborTrees(forest, location); + + assertEquals(0, result.size()); + } }