From 8fa5f0da2418931b16e21530d9bd38f786411e2b Mon Sep 17 00:00:00 2001 From: Danny McCarragher Date: Wed, 23 Apr 2025 21:04:36 -0700 Subject: [PATCH 1/5] Helper Method for finding possible Fire Moves --- src/Fire.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..8f22aee 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -40,4 +40,26 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { // just a location. What other information might be useful? return -1; } + + public static void timetoBurnMoves(char[][] forest, int row, int col){ + + if(row < 0 || col < 0 || row >= forest.length || col >= forest[0].length || forest[row][col] != 't'){ + return; + } + + //mark as a road to not visit again + forest[row][col] = '.'; + + int[][] directions = { + {1,0}, + {-1,0}, + {0,-1}, + {0,1} + }; + + for(int[] direction : directions){ + timetoBurnMoves(forest, row + direction[0], col + direction[1]); + } + + } } \ No newline at end of file From c127fb1c357c5ecc08c47f3177c5fc7ed79a50af Mon Sep 17 00:00:00 2001 From: Danny McCarragher Date: Wed, 23 Apr 2025 21:28:00 -0700 Subject: [PATCH 2/5] Initial Attempt at timeToBurn Method --- src/Fire.java | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 8f22aee..821d13b 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,6 @@ +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 @@ -38,7 +41,30 @@ 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; + + // base case for if it is not a tree + if(forest[matchR][matchC] != 't'){ + return 0; + } + + // construct linked list of integer array + Queue queue = new LinkedList<>(); + + // add the rowm column, and 0 (time) to queue + queue.add(new int[] {matchR, matchC, 0}); + + while (!queue.isEmpty()) { + int[] current = queue.poll(); + + int curR = current[0]; + int curC = current[1]; + int currentTime = current[3]; + + timetoBurnMoves(forest, curR, curC); + + if(currentTime> ) + + } } public static void timetoBurnMoves(char[][] forest, int row, int col){ From 0a1577294ad5995dd9a05e1fc4e6e89010a4e42b Mon Sep 17 00:00:00 2001 From: Danny McCarragher Date: Wed, 23 Apr 2025 21:40:25 -0700 Subject: [PATCH 3/5] Rework of timeToBurnMoves method. Including time as a parameter --- src/Fire.java | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 821d13b..58b206b 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,4 +1,6 @@ +import java.util.ArrayList; import java.util.LinkedList; +import java.util.List; import java.util.Queue; public class Fire { @@ -47,6 +49,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { return 0; } + int time = 0; // construct linked list of integer array Queue queue = new LinkedList<>(); @@ -56,25 +59,28 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { while (!queue.isEmpty()) { int[] current = queue.poll(); - int curR = current[0]; - int curC = current[1]; - int currentTime = current[3]; + int row= current[0]; + int col = current[1]; + int currentTime = current[2]; - timetoBurnMoves(forest, curR, curC); + if(currentTime > time){ + time = currentTime; + } - if(currentTime> ) + // add all valid trees to queue + for(int[] nextTrees : timetoBurnMoves(forest, row,col, currentTime)){ + queue.add(nextTrees); + } } - } - public static void timetoBurnMoves(char[][] forest, int row, int col){ + return time; + } - if(row < 0 || col < 0 || row >= forest.length || col >= forest[0].length || forest[row][col] != 't'){ - return; - } + public static List timetoBurnMoves(char[][] forest, int row, int col, int time){ - //mark as a road to not visit again - forest[row][col] = '.'; + // create list for the next trees to burn + List nextTrees = new ArrayList<>(); int[][] directions = { {1,0}, @@ -84,8 +90,20 @@ public static void timetoBurnMoves(char[][] forest, int row, int col){ }; for(int[] direction : directions){ - timetoBurnMoves(forest, row + direction[0], col + direction[1]); + int newR = row + direction[0]; + int newC = col + direction[1]; + + // validate if direction is within grid and is a tree + if(row >= 0 || col >= 0 || row < forest.length || col < forest[0].length || forest[row][col] == 't'){ + //mark as a road to not visit again + forest[newR][newC] = '.'; + //add the tree to list + nextTrees.add(new int[]{newR, newC, time++}); + + } } + + return nextTrees; } } \ No newline at end of file From ba23edc492b52f6fdb9d67d78b534f304f3de848 Mon Sep 17 00:00:00 2001 From: Danny McCarragher Date: Wed, 23 Apr 2025 21:46:35 -0700 Subject: [PATCH 4/5] Completed Fire. Test Passing. --- src/Fire.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 58b206b..52443c3 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -53,7 +53,8 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { // construct linked list of integer array Queue queue = new LinkedList<>(); - // add the rowm column, and 0 (time) to queue + // add the row column, and 0 (time) to queue + // first tree starts burning at time 0 queue.add(new int[] {matchR, matchC, 0}); while (!queue.isEmpty()) { @@ -63,11 +64,12 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { int col = current[1]; int currentTime = current[2]; + // track the latest time any tree starts burning if(currentTime > time){ time = currentTime; } - // add all valid trees to queue + // add all valid neighbor trees to queue for(int[] nextTrees : timetoBurnMoves(forest, row,col, currentTime)){ queue.add(nextTrees); } @@ -94,11 +96,11 @@ public static List timetoBurnMoves(char[][] forest, int row, int col, int int newC = col + direction[1]; // validate if direction is within grid and is a tree - if(row >= 0 || col >= 0 || row < forest.length || col < forest[0].length || forest[row][col] == 't'){ + if(newR >= 0 && newC >= 0 && newR < forest.length && newC < forest[0].length && forest[newR][newC] == 't'){ //mark as a road to not visit again forest[newR][newC] = '.'; //add the tree to list - nextTrees.add(new int[]{newR, newC, time++}); + nextTrees.add(new int[]{newR, newC, time + 1}); } } From 7e241f74c867cc2003975bb27351cb389b97b5ce Mon Sep 17 00:00:00 2001 From: dannymccarragher Date: Thu, 24 Apr 2025 12:05:54 -0700 Subject: [PATCH 5/5] Wrote 2 tests for testing timeToBurn --- src/FireTest.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..1f84456 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,38 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testTimeToBurn_OneSurroundedTree(){ + char[][] forest = { + {'t', '.', 't'}, + {'.', 't', '.'}, + {'t', '.', 't'} + }; + + int matchR = 1; + int matchC = 1; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + + + } + + @Test + public void testTimeToBurn_SingleRowOfTrees(){ + char[][] forest = { + {'t', 't', 't'} + }; + + int matchR = 0; + int matchC = 0; + + int expected = 2; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } }