From 1eeace5fa1ecabed6e255ceb4a79bc0c3a594d5b Mon Sep 17 00:00:00 2001 From: Kabul Totakhil Date: Thu, 5 Jun 2025 11:07:19 -0700 Subject: [PATCH 1/6] add the direction code --- src/Fire.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..1bad448 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,28 @@ 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 rows = forest.length; + + int cols = forest[0].length; + + + int[][] directions = { + { -1, 0 }, // up + { 1, 0 }, // down + { 0, -1}, // left + { 0, 1 } // right + }; + + + + Queue queue = new LinkedList<>(); + + boolean[][] visited = new boolean[rows][cols]; + + return -1; } } \ No newline at end of file From dc33b7b6f79d65d3b9f2ef4f3ed846bb1906c1b1 Mon Sep 17 00:00:00 2001 From: Kabul Totakhil Date: Thu, 5 Jun 2025 11:10:15 -0700 Subject: [PATCH 2/6] change the direction code to the correct form --- src/Fire.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 1bad448..b475172 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -50,16 +50,16 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { int[][] directions = { - { -1, 0 }, // up - { 1, 0 }, // down - { 0, -1}, // left - { 0, 1 } // right + {0, 1}, + {1, 0}, + {0, -1}, + {-1, 0} }; }; Queue queue = new LinkedList<>(); - + boolean[][] visited = new boolean[rows][cols]; From 5f6dff4535a69ef0e542a0165aa756f86a0978ac Mon Sep 17 00:00:00 2001 From: Alex Borovets Date: Thu, 5 Jun 2025 11:11:09 -0700 Subject: [PATCH 3/6] organized it a little bit --- src/Fire.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 1bad448..58a7da5 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -45,8 +45,8 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { int rows = forest.length; - int cols = forest[0].length; + boolean[][] visited = new boolean[rows][cols]; int[][] directions = { @@ -60,7 +60,6 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { Queue queue = new LinkedList<>(); - boolean[][] visited = new boolean[rows][cols]; return -1; From f4c3dc4d2410a28880ef3edc387c23afc5420238 Mon Sep 17 00:00:00 2001 From: Alex Borovets Date: Thu, 5 Jun 2025 11:17:38 -0700 Subject: [PATCH 4/6] fixed some errors --- src/Fire.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 5d6c289..f0a2cc0 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -49,18 +49,15 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { boolean[][] visited = new boolean[rows][cols]; - int[][] directions = { - {0, 1}, - {1, 0}, - {0, -1}, - {-1, 0} }; - }; + int[] dr = {-1, 1, 0, 0}; + int[] dc = {0, 0, -1, 1}; - Queue queue = new LinkedList<>(); - - boolean[][] visited = new boolean[rows][cols]; + queue.add(new int[] {matchR, matchC, 0}); + visited[matchR][matchC] = true; + + int maxTime = 0; return -1; From 5fda5e880c248b059513ea60773b5ebc43c17ad2 Mon Sep 17 00:00:00 2001 From: Kabul Totakhil Date: Thu, 5 Jun 2025 11:31:20 -0700 Subject: [PATCH 5/6] add direction --- src/Fire.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index f0a2cc0..9219853 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -49,8 +49,15 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { boolean[][] visited = new boolean[rows][cols]; - int[] dr = {-1, 1, 0, 0}; - int[] dc = {0, 0, -1, 1}; + + + int [][] direction = { + {-1,0}, + {1,0}, + {0,0}, + {0,0} + + }; Queue queue = new LinkedList<>(); From 8370bc02349b52ca965f9a5f0b304fb3a6f8b320 Mon Sep 17 00:00:00 2001 From: Alex Borovets Date: Tue, 10 Jun 2025 14:47:25 -0700 Subject: [PATCH 6/6] finished --- src/Fire.java | 56 +++++++++++++++++++++++++++++++++-------------- src/FireTest.java | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 17 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 9219853..e3a4b4e 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -46,27 +46,49 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { int rows = forest.length; int cols = forest[0].length; + boolean[][] visited = new boolean[rows][cols]; - - - - - int [][] direction = { - {-1,0}, - {1,0}, - {0,0}, - {0,0} - + + // Check that starting point is a tree + if (forest[matchR][matchC] != 't') { + return -1; + } + + int[][] directions = { + {-1, 0}, // up + {1, 0}, // down + {0, -1}, // left + {0, 1} // right }; - - + Queue queue = new LinkedList<>(); - queue.add(new int[] {matchR, matchC, 0}); + queue.add(new int[]{matchR, matchC, 0}); visited[matchR][matchC] = true; - + int maxTime = 0; - - - return -1; + + while (!queue.isEmpty()) { + int[] current = queue.poll(); + int r = current[0]; + int c = current[1]; + int time = current[2]; + + maxTime = Math.max(maxTime, time); + + for (int[] dir : directions) { + int newR = r + dir[0]; + int newC = c + dir[1]; + + 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}); + } + } + } + + return maxTime; } } \ No newline at end of file diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..2ed215d 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,58 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testSingleTree() { + char[][] forest = { + {'t'} + }; + assertEquals(0, Fire.timeToBurn(forest, 0, 0)); + } + + @Test + public void testSingleEmptySpace() { + char[][] forest = { + {'.'} + }; + assertEquals(-1, Fire.timeToBurn(forest, 0, 0)); + } + + @Test + public void testDisconnectedTrees() { + char[][] forest = { + {'t', '.', 't'}, + {'.', '.', '.'}, + {'t', '.', 't'} + }; + assertEquals(0, Fire.timeToBurn(forest, 0, 0)); + } + + @Test + public void testFullBlockOfTrees() { + char[][] forest = { + {'t', 't'}, + {'t', 't'} + }; + assertEquals(2, Fire.timeToBurn(forest, 0, 0)); + } + + @Test + public void testFireStartsOnGround() { + char[][] forest = { + {'t', 't'}, + {'.', 't'} + }; + assertEquals(-1, Fire.timeToBurn(forest, 1, 0)); + } + + @Test + public void testEdgeSpread() { + char[][] forest = { + {'t', 't', 't', '.', '.'}, + {'.', '.', 't', '.', '.'}, + {'.', '.', 't', 't', 't'} + }; + assertEquals(6, Fire.timeToBurn(forest, 0, 0)); + } }