From 6244397dc5828a22ce2fdcdcbe47d0ba7bcf19b0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 29 Apr 2025 12:09:58 -0700 Subject: [PATCH 1/2] Started working on timeToBurn method --- src/Fire.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..245d906 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -38,6 +38,8 @@ 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? + + //started working on the timeToBurn method return -1; } } \ No newline at end of file From e41b1cdfb5c64d38dfb6272be2a2e08d441c91f6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 4 May 2025 00:01:25 -0700 Subject: [PATCH 2/2] done with forest fire --- src/Fire.java | 67 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 245d906..4eeee8a 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,8 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a @@ -38,8 +43,66 @@ 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? + if (forest[matchR][matchC] != 't') { + return 0; + } + + + int[] start = new int[]{matchR, matchC, 0}; + boolean[][] burnt = new boolean[forest.length][forest[0].length]; + + Queue queue = new LinkedList<>(); + queue.add(start); + + int maxTime = 0; + + while(!queue.isEmpty()){ + int[] current = queue.poll(); + + int row = current[0]; + int col = current[1]; + int time = current[2]; + + // if the current tree is down continue + if (burnt[row][col]){ + continue; + } + + burnt[row][col] = true; + + List moves = possibleMoves(forest, row, col); + + // add reachable tree to the queue + for (var move : moves){ + int[] toAdd = new int[]{move[0],move[1], time + 1}; + queue.add(toAdd); + } + + if( time > maxTime){ + maxTime = time; + } + } + return maxTime; + } + + public static List possibleMoves(char[][] forest, int row, int col) { + List moves = new ArrayList<>(); + int[][] directions = { + {-1, 0}, // up + { 1, 0}, // down + { 0,-1}, // left + { 0, 1} // right + }; + + for (int[] d : directions) { + int nr = row + d[0], nc = col + d[1]; + if (nr >= 0 && nr < forest.length && + nc >= 0 && nc < forest[0].length && + forest[nr][nc] == 't') { + moves.add(new int[]{nr, nc}); + } + } - //started working on the timeToBurn method - return -1; + return moves; } } \ No newline at end of file