From 1c38f667228227d6d426c612e10a0ed416312a45 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 5 Jun 2025 11:11:16 -0700 Subject: [PATCH 1/9] possible tree locations implemeted --- src/Fire.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..8f7caa7 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,6 @@ +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 @@ -40,4 +43,36 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { // just a location. What other information might be useful? return -1; } + + public static List possibleBurns(char[][] forest, int matchR, int matchC) + { + List possibleList = new ArrayList<>(); + + int[][] possibleBurn = + { + {1,0}, + {-1,0}, + {0,1}, + {0,-1} + }; + + int newRow = 0; + int newCol = 0; + + for(int[] possible : possibleBurn) + { + newRow = matchR + possible[0]; + newCol = matchC + possible[1]; + + if(newRow >= 0 && newRow < + forest.length && + newCol >= 0 && + newCol < forest[matchR].length && + forest[matchR][matchC] == 't') + { + possibleList.add(new int[] {newRow, newCol}); + } + } + return possibleList; + } } \ No newline at end of file From 12f0976790094b8fd55b417c8b0c8b474a39261d Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:21:55 -0700 Subject: [PATCH 2/9] initial test for possibleBurns --- src/Fire.java | 2 +- src/FireTest.java | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 8f7caa7..1a13853 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -70,7 +70,7 @@ public static List possibleBurns(char[][] forest, int matchR, int matchC) newCol < forest[matchR].length && forest[matchR][matchC] == 't') { - possibleList.add(new int[] {newRow, newCol}); + possibleList.add(new int[]{newRow, newCol}); } } return possibleList; diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..3458f4b 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -1,5 +1,11 @@ +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import org.junit.jupiter.api.Test; public class FireTest { @@ -20,4 +26,38 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testGetPossibleBurns() { + char[][] forest = { + {'t','.','.','t','t','t','t','.','t'}, + {'.','.','t','t','.','.','.','.','t'}, + {'.','.','t','t','t','t','t','t','t'}, + {'t','t','t','t','.','.','.','.','.'} + }; + + int matchR = 2; + int matchC = 3; + + List possibleBurn = Fire.possibleBurns(forest, matchR, matchC); + Set convertedSet = convertToSet(possibleBurn); + + assertEquals(4, convertedSet.size()); + assertTrue(convertedSet.contains("2, 2")); + assertTrue(convertedSet.contains("1, 3")); + assertTrue(convertedSet.contains("2, 4")); + assertTrue(convertedSet.contains("3, 3")); + assertFalse(convertedSet.contains("3, 0")); + } + + // Set method to convert list to set to check what it contains + private Set convertToSet(List list) { + Set converted = new HashSet(); + + for (int[] arr : list) { + converted.add(arr[0] + ", " + arr[1]); + } + + return converted; + } } From ef39fec9dedfc827ac36b5c9a127474686766b09 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Mon, 9 Jun 2025 11:36:21 -0700 Subject: [PATCH 3/9] queue implementation --- src/Fire.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 1a13853..9f5f567 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,5 +1,7 @@ import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; +import java.util.Queue; public class Fire { /** @@ -39,9 +41,20 @@ public class Fire { * @return the time at which the final tree to be incinerated starts burning */ public static int timeToBurn(char[][] forest, int matchR, int matchC) { + boolean [][] visited = new boolean[forest.length][forest[0].length]; + Queue queue = new LinkedList<>(); + int[] myNum = {matchR,matchC,0}; + + queue.add(myNum); + + myNum[2]++; + while(!queue.isEmpty()) + { + queue.poll(); + } // HINT: when adding to your BFS queue, you can include more information than // just a location. What other information might be useful? - return -1; + return myNum[2]; } public static List possibleBurns(char[][] forest, int matchR, int matchC) From 028c44d99a6290b161dc9368e39467b41a4d29dd Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 9 Jun 2025 12:24:00 -0700 Subject: [PATCH 4/9] Initial solution for timeToBurn --- src/Fire.java | 30 +++++++++++++++++++++++------- src/FireTest.java | 44 ++++++++++++++++++++++---------------------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 9f5f567..54f5f27 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -41,23 +41,38 @@ public class Fire { * @return the time at which the final tree to be incinerated starts burning */ public static int timeToBurn(char[][] forest, int matchR, int matchC) { - boolean [][] visited = new boolean[forest.length][forest[0].length]; + boolean[][] visited = new boolean[forest.length][forest[0].length]; Queue queue = new LinkedList<>(); - int[] myNum = {matchR,matchC,0}; + int burnTime = 0; + int[] myNum = {matchR,matchC,burnTime}; queue.add(myNum); - myNum[2]++; while(!queue.isEmpty()) { - queue.poll(); + int[] treeBurn = queue.poll(); + int curR = treeBurn[0]; + int curC = treeBurn[1]; + int time = treeBurn[2]; + + if (visited[curR][curC]) continue; + + burnTime = time; + + visited[curR][curC] = true; + + List neighborTrees = possibleBurns(forest, curR, curC, time); + + if (neighborTrees != null) { + queue.addAll(neighborTrees); + } } // HINT: when adding to your BFS queue, you can include more information than // just a location. What other information might be useful? - return myNum[2]; + return burnTime; } - public static List possibleBurns(char[][] forest, int matchR, int matchC) + public static List possibleBurns(char[][] forest, int matchR, int matchC, int time) { List possibleList = new ArrayList<>(); @@ -71,6 +86,7 @@ public static List possibleBurns(char[][] forest, int matchR, int matchC) int newRow = 0; int newCol = 0; + int newTime = time + 1; for(int[] possible : possibleBurn) { @@ -83,7 +99,7 @@ public static List possibleBurns(char[][] forest, int matchR, int matchC) newCol < forest[matchR].length && forest[matchR][matchC] == 't') { - possibleList.add(new int[]{newRow, newCol}); + possibleList.add(new int[]{newRow, newCol, newTime}); } } return possibleList; diff --git a/src/FireTest.java b/src/FireTest.java index 3458f4b..728c816 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -27,28 +27,28 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } - @Test - public void testGetPossibleBurns() { - char[][] forest = { - {'t','.','.','t','t','t','t','.','t'}, - {'.','.','t','t','.','.','.','.','t'}, - {'.','.','t','t','t','t','t','t','t'}, - {'t','t','t','t','.','.','.','.','.'} - }; - - int matchR = 2; - int matchC = 3; - - List possibleBurn = Fire.possibleBurns(forest, matchR, matchC); - Set convertedSet = convertToSet(possibleBurn); - - assertEquals(4, convertedSet.size()); - assertTrue(convertedSet.contains("2, 2")); - assertTrue(convertedSet.contains("1, 3")); - assertTrue(convertedSet.contains("2, 4")); - assertTrue(convertedSet.contains("3, 3")); - assertFalse(convertedSet.contains("3, 0")); - } + // @Test + // public void testGetPossibleBurns() { + // char[][] forest = { + // {'t','.','.','t','t','t','t','.','t'}, + // {'.','.','t','t','.','.','.','.','t'}, + // {'.','.','t','t','t','t','t','t','t'}, + // {'t','t','t','t','.','.','.','.','.'} + // }; + + // int matchR = 2; + // int matchC = 3; + + // List possibleBurn = Fire.possibleBurns(forest, matchR, matchC); + // Set convertedSet = convertToSet(possibleBurn); + + // assertEquals(4, convertedSet.size()); + // assertTrue(convertedSet.contains("2, 2")); + // assertTrue(convertedSet.contains("1, 3")); + // assertTrue(convertedSet.contains("2, 4")); + // assertTrue(convertedSet.contains("3, 3")); + // assertFalse(convertedSet.contains("3, 0")); + // } // Set method to convert list to set to check what it contains private Set convertToSet(List list) { From 0ad482d4ff76d44eef35165bdd5f42c00d031e03 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Mon, 9 Jun 2025 12:36:55 -0700 Subject: [PATCH 5/9] Implementing test cases --- src/FireTest.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index 728c816..e070c66 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -27,6 +27,40 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + @Test + public void testTimeNormal() { + char[][] forest = { + {'t','t','t','t','t'}, + {'t','t','t','t','t'}, + {'t','t','t','.','.'} + }; + + int matchR = 1; + int matchC = 2; + + int expected = 3; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void testTimeZero() { + char[][] forest = { + {'t','.','t','t','t'}, + {'.','.','t','t','t'}, + {'t','t','t','t','t'} + }; + + int matchR = 0; + int matchC = 0; + + int expected = 1; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + // @Test // public void testGetPossibleBurns() { // char[][] forest = { From 79524f820a202fb736152d1a6ea4bd052790dbce Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 9 Jun 2025 12:43:17 -0700 Subject: [PATCH 6/9] Better solution for timeToBurn --- src/Fire.java | 15 ++++++++++----- src/FireTest.java | 44 ++++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 54f5f27..6ef31c3 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -61,10 +61,16 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { visited[curR][curC] = true; - List neighborTrees = possibleBurns(forest, curR, curC, time); + List neighborTrees = possibleBurns(forest, curR, curC); + + time++; if (neighborTrees != null) { - queue.addAll(neighborTrees); + for (int[] neighbor : neighborTrees) { + int neighR = neighbor[0]; + int neighC = neighbor[1]; + queue.add(new int[]{neighR, neighC, time}); + } } } // HINT: when adding to your BFS queue, you can include more information than @@ -72,7 +78,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { return burnTime; } - public static List possibleBurns(char[][] forest, int matchR, int matchC, int time) + public static List possibleBurns(char[][] forest, int matchR, int matchC) { List possibleList = new ArrayList<>(); @@ -86,7 +92,6 @@ public static List possibleBurns(char[][] forest, int matchR, int matchC, int newRow = 0; int newCol = 0; - int newTime = time + 1; for(int[] possible : possibleBurn) { @@ -99,7 +104,7 @@ public static List possibleBurns(char[][] forest, int matchR, int matchC, newCol < forest[matchR].length && forest[matchR][matchC] == 't') { - possibleList.add(new int[]{newRow, newCol, newTime}); + possibleList.add(new int[]{newRow, newCol}); } } return possibleList; diff --git a/src/FireTest.java b/src/FireTest.java index e070c66..d973ce9 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -61,28 +61,28 @@ public void testTimeZero() { assertEquals(expected, actual); } - // @Test - // public void testGetPossibleBurns() { - // char[][] forest = { - // {'t','.','.','t','t','t','t','.','t'}, - // {'.','.','t','t','.','.','.','.','t'}, - // {'.','.','t','t','t','t','t','t','t'}, - // {'t','t','t','t','.','.','.','.','.'} - // }; - - // int matchR = 2; - // int matchC = 3; - - // List possibleBurn = Fire.possibleBurns(forest, matchR, matchC); - // Set convertedSet = convertToSet(possibleBurn); - - // assertEquals(4, convertedSet.size()); - // assertTrue(convertedSet.contains("2, 2")); - // assertTrue(convertedSet.contains("1, 3")); - // assertTrue(convertedSet.contains("2, 4")); - // assertTrue(convertedSet.contains("3, 3")); - // assertFalse(convertedSet.contains("3, 0")); - // } + @Test + public void testGetPossibleBurns() { + char[][] forest = { + {'t','.','.','t','t','t','t','.','t'}, + {'.','.','t','t','.','.','.','.','t'}, + {'.','.','t','t','t','t','t','t','t'}, + {'t','t','t','t','.','.','.','.','.'} + }; + + int matchR = 2; + int matchC = 3; + + List possibleBurn = Fire.possibleBurns(forest, matchR, matchC); + Set convertedSet = convertToSet(possibleBurn); + + assertEquals(4, convertedSet.size()); + assertTrue(convertedSet.contains("2, 2")); + assertTrue(convertedSet.contains("1, 3")); + assertTrue(convertedSet.contains("2, 4")); + assertTrue(convertedSet.contains("3, 3")); + assertFalse(convertedSet.contains("3, 0")); + } // Set method to convert list to set to check what it contains private Set convertToSet(List list) { From 0bf4c4c99d60f8d0b8eec38959e9e349a89730c8 Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 9 Jun 2025 13:15:57 -0700 Subject: [PATCH 7/9] Fixed bug in possibleBurns --- src/Fire.java | 10 +++++----- src/FireTest.java | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 6ef31c3..0e3f81e 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -57,10 +57,10 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { if (visited[curR][curC]) continue; - burnTime = time; - visited[curR][curC] = true; + burnTime = time; + List neighborTrees = possibleBurns(forest, curR, curC); time++; @@ -69,7 +69,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { for (int[] neighbor : neighborTrees) { int neighR = neighbor[0]; int neighC = neighbor[1]; - queue.add(new int[]{neighR, neighC, time}); + if (!visited[neighR][neighC]) queue.add(new int[]{neighR, neighC, time}); } } } @@ -101,8 +101,8 @@ public static List possibleBurns(char[][] forest, int matchR, int matchC) if(newRow >= 0 && newRow < forest.length && newCol >= 0 && - newCol < forest[matchR].length && - forest[matchR][matchC] == 't') + newCol < forest[newRow].length && + forest[newRow][newCol] == 't') { possibleList.add(new int[]{newRow, newCol}); } diff --git a/src/FireTest.java b/src/FireTest.java index d973ce9..626a5fc 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -55,7 +55,7 @@ public void testTimeZero() { int matchR = 0; int matchC = 0; - int expected = 1; + int expected = 0; int actual = Fire.timeToBurn(forest, matchR, matchC); assertEquals(expected, actual); From eeff44874e0a41672ce27d4e31a6ba69b1069090 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Mon, 9 Jun 2025 13:30:09 -0700 Subject: [PATCH 8/9] Testing after bug fix --- src/FireTest.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index 626a5fc..afcd28b 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -61,6 +61,24 @@ public void testTimeZero() { assertEquals(expected, actual); } + @Test + public void testTimeAllPeriod() { + char[][] forest = { + {'.','.','.','.','.'}, + {'.','.','.','.','.'}, + {'.','.','.','.','.'} + }; + + int matchR = 2; + int matchC = 4; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test public void testGetPossibleBurns() { char[][] forest = { @@ -84,6 +102,22 @@ public void testGetPossibleBurns() { assertFalse(convertedSet.contains("3, 0")); } + @Test + public void testGetEmptyPossibleBurns() + { + char[][] forest = { + {'.','.','.','.','.','.','.','.','.'}, + {'.','.','.','.','.','.','.','.','.'}, + {'.','.','.','.','.','.','.','.','.'}, + {'.','.','.','.','.','.','.','.','.'} + }; + int matchR = 3; + int matchC = 3; + + List possibleBurn = Fire.possibleBurns(forest, matchR, matchC); + assertEquals(0,possibleBurn.size()); + } + // Set method to convert list to set to check what it contains private Set convertToSet(List list) { Set converted = new HashSet(); From f78844c9c8af3a9a2c0d112835edff02f88f2d3f Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 9 Jun 2025 13:44:51 -0700 Subject: [PATCH 9/9] Added more tests --- src/FireTest.java | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index afcd28b..8a1d42f 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -44,6 +44,23 @@ public void testTimeNormal() { assertEquals(expected, actual); } + @Test + public void testTimeAllTrees() { + char[][] forest = { + {'t','t','t','t','t'}, + {'t','t','t','t','t'}, + {'t','t','t','t','t'} + }; + + int matchR = 0; + int matchC = 0; + + int expected = 6; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + @Test public void testTimeZero() { char[][] forest = { @@ -118,6 +135,44 @@ public void testGetEmptyPossibleBurns() assertEquals(0,possibleBurn.size()); } + @Test + public void testGetTwoPossibleBurns() { + char[][] forest = { + {'.','.','.','.','.'}, + {'.','.','t','t','t'}, + {'.','.','.','.','.'} + }; + int matchR = 1; + int matchC = 3; + List possibleBurn = Fire.possibleBurns(forest, matchR, matchC); + Set convertedSet = convertToSet(possibleBurn); + + assertEquals(2, convertedSet.size()); + assertTrue(convertedSet.contains("1, 2")); + assertTrue(convertedSet.contains("1, 4")); + assertFalse(convertedSet.contains("0, 3")); + assertFalse(convertedSet.contains("2, 3")); + } + + @Test + public void testGetThreePossibleBurns() { + char[][] forest = { + {'.','.','.','t','.'}, + {'.','.','t','t','t'}, + {'.','.','.','.','.'} + }; + int matchR = 1; + int matchC = 3; + List possibleBurn = Fire.possibleBurns(forest, matchR, matchC); + Set convertedSet = convertToSet(possibleBurn); + + assertEquals(3, convertedSet.size()); + assertTrue(convertedSet.contains("1, 2")); + assertTrue(convertedSet.contains("1, 4")); + assertTrue(convertedSet.contains("0, 3")); + assertFalse(convertedSet.contains("2, 3")); + } + // Set method to convert list to set to check what it contains private Set convertToSet(List list) { Set converted = new HashSet();