From e181c34ddff7444d980a9f005c9744c3fbdd3e03 Mon Sep 17 00:00:00 2001 From: TavSingh Date: Tue, 22 Apr 2025 14:28:04 -0700 Subject: [PATCH 1/4] Adding helper method --- src/Fire.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..91a51e6 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,6 +41,29 @@ 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? + + //int counter + + return -1; } + + private static void directions(char[][] forest, int r, int c) { + Queue queue = new LinkedList<>(); + + //map of directions + int[][] direction = new int[][] { + {-1,0}, + {1,0}, + {0,-1}, + {0,1} + }; + + //while not empty + while(!queue.isEmpty()) { + for (int[] dir : direction) { + + } + } + } } \ No newline at end of file From 20e9b9e2c8b428ef00f1ec1096a03bd3eabd1847 Mon Sep 17 00:00:00 2001 From: Madeleine Chance Date: Tue, 22 Apr 2025 14:40:20 -0700 Subject: [PATCH 2/4] working on directions helper method --- src/Fire.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 91a51e6..ee6d861 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -48,7 +48,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { return -1; } - private static void directions(char[][] forest, int r, int c) { + private static int directions(char[][] forest, int r, int c) { Queue queue = new LinkedList<>(); //map of directions @@ -59,11 +59,28 @@ private static void directions(char[][] forest, int r, int c) { {0,1} }; + queue.offer(new int[]{r, c}); + + int counter = 0; + //while not empty while(!queue.isEmpty()) { + + int[] curr = queue.poll(); + for (int[] dir : direction) { + int newR = curr[0] + dir[0]; + int newC = curr[1] + dir[1]; + if (newR >= 0 && newR < forest.length && + newC >= 0 && newC < forest[0].length && + forest[newR][newC] == 't'){ + queue.offer(new int[]{newR, newC}); + } } + counter++; } + + return counter; } } \ No newline at end of file From 6eb69f303fa8abb59f4a62a594b62cc3693676bb Mon Sep 17 00:00:00 2001 From: TavSingh Date: Tue, 22 Apr 2025 14:49:34 -0700 Subject: [PATCH 3/4] experimenting with while loop --- src/Fire.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index ee6d861..1e918fc 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -45,7 +45,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { //int counter - return -1; + return directions(forest, matchR, matchC); } private static int directions(char[][] forest, int r, int c) { @@ -68,6 +68,8 @@ private static int directions(char[][] forest, int r, int c) { int[] curr = queue.poll(); + forest[curr[0]][curr[1]] = 'f'; + for (int[] dir : direction) { int newR = curr[0] + dir[0]; int newC = curr[1] + dir[1]; @@ -76,9 +78,9 @@ private static int directions(char[][] forest, int r, int c) { newC >= 0 && newC < forest[0].length && forest[newR][newC] == 't'){ queue.offer(new int[]{newR, newC}); + counter++; } } - counter++; } return counter; From 443971face47f41a5dd4af366df066ea3586df4c Mon Sep 17 00:00:00 2001 From: Madeleine Chance Date: Tue, 22 Apr 2025 14:56:50 -0700 Subject: [PATCH 4/4] implemented timeToBurn --- src/Fire.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 1e918fc..86b0edc 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -59,15 +59,18 @@ private static int directions(char[][] forest, int r, int c) { {0,1} }; - queue.offer(new int[]{r, c}); + queue.offer(new int[]{r, c, -1}); int counter = 0; + //while not empty while(!queue.isEmpty()) { int[] curr = queue.poll(); + int count = curr[2] + 1; + forest[curr[0]][curr[1]] = 'f'; for (int[] dir : direction) { @@ -77,10 +80,11 @@ private static int directions(char[][] forest, int r, int c) { if (newR >= 0 && newR < forest.length && newC >= 0 && newC < forest[0].length && forest[newR][newC] == 't'){ - queue.offer(new int[]{newR, newC}); - counter++; + queue.offer(new int[]{newR, newC, count}); } } + + counter = count; } return counter;