From 089bcd44dfeef872fcffd92c654beb1613c6d634 Mon Sep 17 00:00:00 2001 From: Tyler Gilmore Date: Sun, 27 Apr 2025 14:49:42 -0700 Subject: [PATCH 1/5] Got some starter code up. --- src/Fire.java | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index b968c51..a929e9c 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,7 @@ + +import java.util.LinkedList; +import java.util.Queue; + public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a @@ -35,9 +39,45 @@ public class Fire { * @param matchC The column the match is lit at * @return the time at which the final tree to be incinerated starts burning */ - public static int timeToBurn(char[][] forest, int matchR, int matchC) { + private static final int[][] directions = { + {-1, 0}, + { 1, 0}, + { 0,-1}, + { 0, 1} + }; + + 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? + int rows = forest.length; + int cols = forest[0].length; + Queue queue = new LinkedList<>(); + queue.add(new int[]{matchR, matchC}); + + boolean[][] visited = new boolean[rows][cols]; + + while(!queue.isEmpty()) { + int[] current = + } + + return -1; } + + public static int[] possibleMoves(char[][] forest, char[] current) { + + + return null; + } + + public static int location(char[][] forest, int r, int c) { + for(int r = 0; r < forest.length; r++) { + for(int c = 0; c < forest[0].length; c++) { + new int[]{r, c}; + } + } + + + return 0; + } } \ No newline at end of file From b789fecb14ebe6ff885309dfa972ebfd3f5d04ff Mon Sep 17 00:00:00 2001 From: Tyler Gilmore Date: Sun, 27 Apr 2025 14:54:36 -0700 Subject: [PATCH 2/5] Have to push because of my computer needing to restart. --- src/Fire.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index a929e9c..37f711b 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -51,17 +51,21 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { // just a location. What other information might be useful? int rows = forest.length; int cols = forest[0].length; - Queue queue = new LinkedList<>(); - queue.add(new int[]{matchR, matchC}); + Queue queue = new LinkedList<>(); boolean[][] visited = new boolean[rows][cols]; + + queue.add(new int[]{matchR, matchC}); + visited[matchR][matchC] = true; // Marking the first cell visited + + finalTime = 0; while(!queue.isEmpty()) { int[] current = } - return -1; + return finalTime; } public static int[] possibleMoves(char[][] forest, char[] current) { From 8b716df7ef5d78fb64c73c50e15573048301e83d Mon Sep 17 00:00:00 2001 From: Tyler Gilmore Date: Sun, 27 Apr 2025 17:44:24 -0700 Subject: [PATCH 3/5] deleted the helper methods and completed the method. Now just need to add some test cases. --- src/Fire.java | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 37f711b..9cf09f9 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -40,10 +40,10 @@ public class Fire { * @return the time at which the final tree to be incinerated starts burning */ private static final int[][] directions = { - {-1, 0}, - { 1, 0}, - { 0,-1}, - { 0, 1} + {-1, 0}, // LEFT + { 1, 0}, // RIGHT + { 0,-1}, // UP + { 0, 1} // DOWN }; public static int timeToBurn(char[][] forest, int matchR, int matchC) { @@ -55,33 +55,31 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { Queue queue = new LinkedList<>(); boolean[][] visited = new boolean[rows][cols]; - queue.add(new int[]{matchR, matchC}); + queue.add(new int[]{matchR, matchC, 0}); // Adding the starting cell with the time at 0 visited[matchR][matchC] = true; // Marking the first cell visited - finalTime = 0; + int finalTime = 0; // keep track of the time while(!queue.isEmpty()) { - int[] current = - } - - - return finalTime; - } + int[] current = queue.poll(); // creating a pointer + int r = current[0]; + int c = current[1]; + int time = current[2]; // We set a third variable when dealing with time/steps - public static int[] possibleMoves(char[][] forest, char[] current) { - - - return null; - } + finalTime = Math.max(finalTime, time); // finding the max amount of time + + for(int[] dir : directions) { + int newR = r + dir[0]; + int newC = c + dir[1]; - public static int location(char[][] forest, int r, int c) { - for(int r = 0; r < forest.length; r++) { - for(int c = 0; c < forest[0].length; c++) { - new int[]{r, c}; + if(newR >= 0 && newR < rows && + newC >= 0 && newC < cols && + !visited[newR][newC] && forest[newR][newC] == 't') { + visited[newR][newC] = true; + queue.add(new int[]{newR, newC, time + 1}); // adding this neighbor to the queue with time + 1 + } } } - - - return 0; + return finalTime; } } \ No newline at end of file From 75acf75b4d055dbab537780612964f788ad215fc Mon Sep 17 00:00:00 2001 From: Tyler Gilmore Date: Sun, 27 Apr 2025 17:59:35 -0700 Subject: [PATCH 4/5] Completed a few testcases --- src/FireTest.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..a606e84 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,63 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testTimeToBurn_emptyForest() { + char[][] forest = { + {'.','.','.','.','.'}, + {'.','.','.','.','.'}, + {'.','.','.','.','.'}, + {'.','.','.','.','.'} + }; + + int expected = 0; + int actual = Fire.timeToBurn(forest, 1, 1); + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurn_allTrees() { + 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'} + }; + + int expected = 5; + int actual = Fire.timeToBurn(forest, 3, 2); + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurn_twoColumnsWithTrees() { + char[][] forest = { + {'t','t','.','.','.'}, + {'t','t','.','.','.'}, + {'t','t','.','.','.'}, + {'t','t','.','.','.'}, + {'t','t','.','.','.'} + }; + + int expected = 3; + int actual = Fire.timeToBurn(forest, 2, 0); + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurn_twoColumnsWithTreesButStartMatchInWhiteSpace() { + char[][] forest = { + {'t','t','.','.','.'}, + {'t','t','.','.','.'}, + {'t','t','.','.','.'}, + {'t','t','.','.','.'}, + {'t','t','.','.','.'} + }; + + int expected = 0; + int actual = Fire.timeToBurn(forest, 2, 3); + assertEquals(expected, actual); + } } From 999ea36b1aa123768ffe50c3a36504be232216aa Mon Sep 17 00:00:00 2001 From: Tyler Gilmore Date: Sun, 27 Apr 2025 18:28:42 -0700 Subject: [PATCH 5/5] added a few more test cases. --- src/FireTest.java | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index a606e84..2a79ab6 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -79,4 +79,49 @@ public void testTimeToBurn_twoColumnsWithTreesButStartMatchInWhiteSpace() { int actual = Fire.timeToBurn(forest, 2, 3); assertEquals(expected, actual); } + + @Test + public void testTimeToBurn_diagonalTreesStartingAtTopLeft() { + char[][] forest = { + {'t','.','.','.','.'}, + {'.','t','.','.','.'}, + {'.','.','t','.','.'}, + {'.','.','.','t','.'}, + {'.','.','.','.','t'} + }; + + int expected = 0; + int actual = Fire.timeToBurn(forest, 0, 0); + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurn_treesInTheCenter() { + char[][] forest = { + {'.','.','.','.','.'}, + {'.','.','t','.','.'}, + {'.','t','t','t','.'}, + {'.','.','t','.','.'}, + {'.','.','.','.','.'}, + }; + + int expected = 1; + int actual = Fire.timeToBurn(forest, 2, 2); + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurn_spiralForest() { + char[][] forest = { + {'t','t','t','t','t'}, + {'.','.','.','.','t'}, + {'.','t','t','.','t'}, + {'.','t','.','.','t'}, + {'.','t','t','t','t'}, + }; + + int expected = 14; + int actual = Fire.timeToBurn(forest, 0, 0); + assertEquals(expected, actual); + } }