From ae5d13deb3890d976b2a8c59cbfff00b27ec2574 Mon Sep 17 00:00:00 2001 From: Ben Mojo <155706745+oakes777@users.noreply.github.com> Date: Tue, 22 Apr 2025 14:24:32 -0700 Subject: [PATCH 01/13] jslv wk4 bfs forest fire 1st commit --- src/Fire.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..9ff9620 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,5 @@ +import java.util.*; + public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a @@ -38,6 +40,13 @@ 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? + + //base cases + if (forest == null) throw new NullPointerException("Forest 2D matrix cannot be null!"); + + int[] startPoint = new Array(forest[matchR][matchC]); + + return -1; } } \ No newline at end of file From 2a70634abde6e0e0bc014811cdc564b6c670cd0d Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Apr 2025 14:32:28 -0700 Subject: [PATCH 02/13] Started a while loop and initialized current variables --- src/Fire.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 9ff9620..cb03ceb 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -44,8 +44,20 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { //base cases if (forest == null) throw new NullPointerException("Forest 2D matrix cannot be null!"); - int[] startPoint = new Array(forest[matchR][matchC]); + // + int[] startPoint = new int[](forest[matchR][matchC]); + Queue burningTree = new LinkedList<>(); + + burningTree.add(startPoint); + boolean[][] visited = new boolean[forest.length][forest[0].length]; + + while (!burningTree.isEmpty()) { + int[] current = burningTree.poll(); + int curR = current[0]; + int curC = current[1]; + + } return -1; } From 1f7af9cdff4e71e65497f676b6feb0462d416713 Mon Sep 17 00:00:00 2001 From: Ben Mojo <155706745+oakes777@users.noreply.github.com> Date: Tue, 22 Apr 2025 14:47:21 -0700 Subject: [PATCH 03/13] js/lv started into bfs helper method --- src/Fire.java | 57 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index cb03ceb..e1f4d0b 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -5,15 +5,18 @@ public class Fire { * Returns how long it takes for all vulnerable trees to be set on fire if a * match is lit at a given location. * - * The forest is represented via a rectangular 2d char array where t represents a tree + * The forest is represented via a rectangular 2d char array where t represents + * a tree * and . represents an empty space. * - * At time 0, the tree at location [matchR, matchC] is set on fire. At every subsequent - * time step, any tree that is adjacent (up/down/left/right) to a burning tree is also - * set on fire. + * At time 0, the tree at location [matchR, matchC] is set on fire. At every + * subsequent + * time step, any tree that is adjacent (up/down/left/right) to a burning tree + * is also + * set on fire. * * - * EXAMPLE + * EXAMPLE * forest: * * t..tttt.t @@ -27,12 +30,16 @@ public class Fire { * Result: 8 * * Explanation: - * At time 0, the tree at (2, 6) is set on fire. At time 1, its adjacent trees also catch on fire - * At time 2, the trees adjacent to those catch as well. At time 8, the last tree to catch is at - * (0,6). In this example, there is one tree that never burns, so it is not included in the time calculation. + * At time 0, the tree at (2, 6) is set on fire. At time 1, its adjacent trees + * also catch on fire + * At time 2, the trees adjacent to those catch as well. At time 8, the last + * tree to catch is at + * (0,6). In this example, there is one tree that never burns, so it is not + * included in the time calculation. * * - * @param forest a 2d array where t represents a tree and . represents the ground + * @param forest a 2d array where t represents a tree and . represents the + * ground * @param matchR The row the match is lit at * @param matchC The column the match is lit at * @return the time at which the final tree to be incinerated starts burning @@ -41,24 +48,34 @@ 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? - //base cases - if (forest == null) throw new NullPointerException("Forest 2D matrix cannot be null!"); - + // base cases + if (forest == null) + throw new NullPointerException("Forest 2D matrix cannot be null!"); + // - int[] startPoint = new int[](forest[matchR][matchC]); + int[] startPoint = new int[] { matchR, matchC }; Queue burningTree = new LinkedList<>(); burningTree.add(startPoint); boolean[][] visited = new boolean[forest.length][forest[0].length]; - while (!burningTree.isEmpty()) { - int[] current = burningTree.poll(); - int curR = current[0]; - int curC = current[1]; + // while (!burningTree.isEmpty()) { + // int[] current = burningTree.poll(); + // int curR = current[0]; + // int curC = current[1]; + return bfs(forest, visited, matchR, matchC); + + } + + private static int bfs(char[][] forest, boolean[][] visited, int matchR, int matchC) { + //edge cases + if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; + visited[matchR][matchC] = true; + - } - return -1; + return count; } -} \ No newline at end of file + +} From 3e0d18d3e709246454136f33efa52c35fde8f709 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Apr 2025 15:30:46 -0700 Subject: [PATCH 04/13] Added directions in timetoBurn method --- src/Fire.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index e1f4d0b..f67e4ca 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -52,20 +52,26 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { if (forest == null) throw new NullPointerException("Forest 2D matrix cannot be null!"); - // - int[] startPoint = new int[] { matchR, matchC }; + //directions here up down left right + int[][] directions = { + {1, 0}, + {-1, 0}, + {0, 1}, + {0, -1} + }; + boolean[][] visited = new boolean[forest.length][forest[0].length]; Queue burningTree = new LinkedList<>(); + int[] startPoint = new int[] { matchR, matchC }; + burningTree.add(startPoint); - boolean[][] visited = new boolean[forest.length][forest[0].length]; // while (!burningTree.isEmpty()) { // int[] current = burningTree.poll(); // int curR = current[0]; // int curC = current[1]; return bfs(forest, visited, matchR, matchC); - } private static int bfs(char[][] forest, boolean[][] visited, int matchR, int matchC) { @@ -73,9 +79,6 @@ private static int bfs(char[][] forest, boolean[][] visited, int matchR, int mat if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; visited[matchR][matchC] = true; - - return count; } - } From d0474bdf919833827c580bc9d45914d8e647bf38 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Apr 2025 15:33:03 -0700 Subject: [PATCH 05/13] Uncommented the while loop and rearranged code for organization --- src/Fire.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index f67e4ca..adbccb3 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -67,18 +67,25 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { burningTree.add(startPoint); - // while (!burningTree.isEmpty()) { - // int[] current = burningTree.poll(); - // int curR = current[0]; - // int curC = current[1]; + int countTime = 0; + + while (!burningTree.isEmpty()) { + int[] current = burningTree.poll(); + int curR = current[0]; + int curC = current[1]; + + + return bfs(forest, visited, matchR, matchC); } + } private static int bfs(char[][] forest, boolean[][] visited, int matchR, int matchC) { + //edge cases if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; visited[matchR][matchC] = true; - return count; + return countTime; } -} +} \ No newline at end of file From 7f68f66cae6886166a0d49c8a0a5121520b2da8f Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Apr 2025 15:37:32 -0700 Subject: [PATCH 06/13] Rearrange more code for organization --- src/Fire.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index adbccb3..fd4651b 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -74,7 +74,13 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { int curR = current[0]; int curC = current[1]; - + for(int[] direction : directions) { + int newR = curR + direction[0]; + int newC = curC + direction[1]; + + if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; + visited[matchR][matchC] = true; + } return bfs(forest, visited, matchR, matchC); } @@ -82,10 +88,10 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { } private static int bfs(char[][] forest, boolean[][] visited, int matchR, int matchC) { - //edge cases - if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; - visited[matchR][matchC] = true; + //edge cases + if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; + visited[matchR][matchC] = true; - return countTime; - } + return countTime; + } } \ No newline at end of file From 0232ca1b2ae85dd17c8c224b8583e2db17da06f6 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Apr 2025 17:36:49 -0700 Subject: [PATCH 07/13] Fixed variable names,added possible comments, and fixed other orders --- src/Fire.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index fd4651b..6869e85 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -66,6 +66,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { int[] startPoint = new int[] { matchR, matchC }; burningTree.add(startPoint); + visited[matchR][matchC] = true; int countTime = 0; @@ -74,24 +75,25 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { int curR = current[0]; int curC = current[1]; + //find the max time and compare between the highest number to the current number + + //check all directions for(int[] direction : directions) { int newR = curR + direction[0]; int newC = curC + direction[1]; - if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; - visited[matchR][matchC] = true; + //check the bounds + if (newR < 0 || newC < 0 || newR >= forest.length || newC >= forest[0].length || visited[newR][newC] || forest[newR][newC]=='.') return 0; + visited[newR][newC] = true; } - - return bfs(forest, visited, matchR, matchC); - } - + } + return countTime; } + private static int bfs(char[][] forest, boolean[][] visited, int matchR, int matchC) { //edge cases if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; - visited[matchR][matchC] = true; - return countTime; } } \ No newline at end of file From 3cea187328ac573a5e16d7ab69939d45d957a52c Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Apr 2025 18:13:18 -0700 Subject: [PATCH 08/13] Added more comments, and rearranged the while and for loops --- src/Fire.java | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 6869e85..c6bba1d 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -52,24 +52,9 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { if (forest == null) throw new NullPointerException("Forest 2D matrix cannot be null!"); - //directions here up down left right - int[][] directions = { - {1, 0}, - {-1, 0}, - {0, 1}, - {0, -1} - }; - boolean[][] visited = new boolean[forest.length][forest[0].length]; Queue burningTree = new LinkedList<>(); - - int[] startPoint = new int[] { matchR, matchC }; - - burningTree.add(startPoint); - visited[matchR][matchC] = true; - - int countTime = 0; - + while (!burningTree.isEmpty()) { int[] current = burningTree.poll(); int curR = current[0]; @@ -77,6 +62,8 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { //find the max time and compare between the highest number to the current number + //get the adjacent trees then increment + //check all directions for(int[] direction : directions) { int newR = curR + direction[0]; @@ -87,9 +74,26 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { visited[newR][newC] = true; } } + + // int[] startPoint = new int[] { matchR, matchC }; + // burningTree.add(startPoint); + + visited[matchR][matchC] = true; + int countTime = 0; return countTime; } - + + private static List possibleDirections(char[][] forest, int[] currentlocation){ + //directions here up down left right + int[][] directions = { + {1, 0}, + {-1, 0}, + {0, 1}, + {0, -1} + }; + + } + private static int bfs(char[][] forest, boolean[][] visited, int matchR, int matchC) { //edge cases From 602f72f88e5b0e0df97708d29f16cb24325c3116 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Apr 2025 18:37:02 -0700 Subject: [PATCH 09/13] Got rid of the other private method --- src/Fire.java | 55 ++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index c6bba1d..84c50d3 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -52,52 +52,53 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { if (forest == null) throw new NullPointerException("Forest 2D matrix cannot be null!"); - boolean[][] visited = new boolean[forest.length][forest[0].length]; + //directions here up down left right + int[][] directions = { + //up, down, right, left + {-1, 0}, + {1, 0}, + {0, 1}, + {0, -1} + }; + + int ROW = forest.length; + int COL = forest[0].length; + + boolean[][] visited = new boolean[ROW][COL]; Queue burningTree = new LinkedList<>(); + int countTime = 0; + while (!burningTree.isEmpty()) { int[] current = burningTree.poll(); int curR = current[0]; int curC = current[1]; + int time = current[2]; //find the max time and compare between the highest number to the current number //get the adjacent trees then increment - + List moves = possibleDirections(forest, current, curR, curC); + //check all directions for(int[] direction : directions) { - int newR = curR + direction[0]; - int newC = curC + direction[1]; + int newR = direction[0]; + int newC = direction[1]; + + visited[newR][newC] = true; + burningTree.offer(new int[] {newR, newC, time + 1}); //check the bounds if (newR < 0 || newC < 0 || newR >= forest.length || newC >= forest[0].length || visited[newR][newC] || forest[newR][newC]=='.') return 0; - visited[newR][newC] = true; } } - - // int[] startPoint = new int[] { matchR, matchC }; - // burningTree.add(startPoint); - - visited[matchR][matchC] = true; - int countTime = 0; return countTime; } - private static List possibleDirections(char[][] forest, int[] currentlocation){ - //directions here up down left right - int[][] directions = { - {1, 0}, - {-1, 0}, - {0, 1}, - {0, -1} - }; - - } - - private static int bfs(char[][] forest, boolean[][] visited, int matchR, int matchC) { + // private static int bfs(char[][] forest, boolean[][] visited, int matchR, int matchC) { - //edge cases - if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; - return countTime; - } + // //edge cases + // if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; + // return countTime; + // } } \ No newline at end of file From ef5ee2f836a555b83fb5526507f73c941e4e7ade Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Apr 2025 18:43:34 -0700 Subject: [PATCH 10/13] Debugged and fixed minor errors, then incremented burningTree time --- src/Fire.java | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 84c50d3..d32cbed 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -67,6 +67,9 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { boolean[][] visited = new boolean[ROW][COL]; Queue burningTree = new LinkedList<>(); + burningTree.offer(new int[]{matchR, matchC, 0}); + visited[matchR][matchC] = true; + int countTime = 0; while (!burningTree.isEmpty()) { @@ -76,29 +79,27 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { int time = current[2]; //find the max time and compare between the highest number to the current number + if (time > countTime) { + countTime = time; + } - //get the adjacent trees then increment - List moves = possibleDirections(forest, current, curR, curC); - - //check all directions + //check all directions, this is where fire spreads for(int[] direction : directions) { - int newR = direction[0]; - int newC = direction[1]; + int newR = curR + direction[0]; + int newC = curC + direction[1]; visited[newR][newC] = true; burningTree.offer(new int[] {newR, newC, time + 1}); //check the bounds - if (newR < 0 || newC < 0 || newR >= forest.length || newC >= forest[0].length || visited[newR][newC] || forest[newR][newC]=='.') return 0; + if (newR < 0 || newC < 0 || newR >= forest.length || + newC >= forest[0].length || visited[newR][newC] || + forest[newR][newC]=='.') continue; + + visited[newR][newC] = true; + burningTree.offer(new int[] {newR, newC, time + 1}); } } return countTime; } - - // private static int bfs(char[][] forest, boolean[][] visited, int matchR, int matchC) { - - // //edge cases - // if (matchR<0 || matchC<0 || matchR >= forest.length || matchC >= forest[0].length || visited[matchR][matchC] || forest[matchR][matchC]=='.') return 0; - // return countTime; - // } } \ No newline at end of file From 88a548b56544b423beb64ab3b0093f7bb3a683a3 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Apr 2025 18:49:58 -0700 Subject: [PATCH 11/13] Added more comments and fixed smaller areas in the for loop --- src/Fire.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index d32cbed..1b69362 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -67,11 +67,13 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { boolean[][] visited = new boolean[ROW][COL]; Queue burningTree = new LinkedList<>(); + //start position of the fire burningTree.offer(new int[]{matchR, matchC, 0}); visited[matchR][matchC] = true; int countTime = 0; + //where it starts the fire burns each tree and spreads to adjacnet trees while (!burningTree.isEmpty()) { int[] current = burningTree.poll(); int curR = current[0]; @@ -87,15 +89,13 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { for(int[] direction : directions) { int newR = curR + direction[0]; int newC = curC + direction[1]; - - visited[newR][newC] = true; - burningTree.offer(new int[] {newR, newC, time + 1}); //check the bounds if (newR < 0 || newC < 0 || newR >= forest.length || newC >= forest[0].length || visited[newR][newC] || forest[newR][newC]=='.') continue; + //mark visited, aded to queue, then increment the time visited[newR][newC] = true; burningTree.offer(new int[] {newR, newC, time + 1}); } From 3e0464db7c256713585b409446c0235573441cfc Mon Sep 17 00:00:00 2001 From: Ben Mojo <155706745+oakes777@users.noreply.github.com> Date: Tue, 22 Apr 2025 21:16:08 -0700 Subject: [PATCH 12/13] jslv tweaking code and comments --- src/Fire.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 1b69362..9454ab9 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -51,6 +51,8 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { // base cases if (forest == null) throw new NullPointerException("Forest 2D matrix cannot be null!"); + if (forest[matchR][matchC] != 't') return 0; + //directions here up down left right int[][] directions = { @@ -68,22 +70,21 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { Queue burningTree = new LinkedList<>(); //start position of the fire + //.offer() adds to the queue like .add() but won’t crash if the queue is full burningTree.offer(new int[]{matchR, matchC, 0}); visited[matchR][matchC] = true; - int countTime = 0; + int maxTime = 0; - //where it starts the fire burns each tree and spreads to adjacnet trees + //where it starts the fire burns each tree and spreads to adjacent trees while (!burningTree.isEmpty()) { int[] current = burningTree.poll(); int curR = current[0]; int curC = current[1]; int time = current[2]; - //find the max time and compare between the highest number to the current number - if (time > countTime) { - countTime = time; - } + //Math.max compares countTime to the current number + maxTime = Math.max(maxTime, time); //check all directions, this is where fire spreads for(int[] direction : directions) { @@ -100,6 +101,6 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { burningTree.offer(new int[] {newR, newC, time + 1}); } } - return countTime; + return maxTime; } } \ No newline at end of file From bc93801c3d7a4896d2da6c01ebdd4c2d1841fbb5 Mon Sep 17 00:00:00 2001 From: Ben Mojo <155706745+oakes777@users.noreply.github.com> Date: Tue, 22 Apr 2025 21:32:04 -0700 Subject: [PATCH 13/13] lv/js added unit tests, tweaked variable names and comments main code --- src/Fire.java | 57 +++++++++++++++++++++---------------------- src/FireTest.java | 61 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 83 insertions(+), 35 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 9454ab9..d488a24 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -51,56 +51,57 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { // base cases if (forest == null) throw new NullPointerException("Forest 2D matrix cannot be null!"); - if (forest[matchR][matchC] != 't') return 0; + if (forest[matchR][matchC] != 't') + return 0; - - //directions here up down left right + // initializing directions variable for more elegant code int[][] directions = { - //up, down, right, left - {-1, 0}, - {1, 0}, - {0, 1}, - {0, -1} + // up, down, right, left + { -1, 0 }, + { 1, 0 }, + { 0, 1 }, + { 0, -1 } }; - + // for readability int ROW = forest.length; int COL = forest[0].length; - + // initialize visited to avoid looping and Queue structure boolean[][] visited = new boolean[ROW][COL]; Queue burningTree = new LinkedList<>(); - - //start position of the fire - //.offer() adds to the queue like .add() but won’t crash if the queue is full - burningTree.offer(new int[]{matchR, matchC, 0}); - visited[matchR][matchC] = true; - int maxTime = 0; + // start position of the fire + // .offer() adds to the queue like .add() but won’t crash if the queue is full + burningTree.offer(new int[] { matchR, matchC, 0 }); + visited[matchR][matchC] = true; + // initialize counter + int maxStepCount = 0; - //where it starts the fire burns each tree and spreads to adjacent trees + // perform BFS to spread fire to all reachable trees while (!burningTree.isEmpty()) { int[] current = burningTree.poll(); int curR = current[0]; int curC = current[1]; int time = current[2]; - //Math.max compares countTime to the current number - maxTime = Math.max(maxTime, time); + // Math.max compare (maxStepCount to current time) + maxStepCount = Math.max(maxStepCount, time); - //check all directions, this is where fire spreads - for(int[] direction : directions) { + // iterate all cardinal directions + for (int[] direction : directions) { int newR = curR + direction[0]; int newC = curC + direction[1]; - //check the bounds - if (newR < 0 || newC < 0 || newR >= forest.length || - newC >= forest[0].length || visited[newR][newC] || - forest[newR][newC]=='.') continue; + // edge cases: check the bounds, visited, treeless cells + if (newR < 0 || newC < 0 || newR >= forest.length || + newC >= forest[0].length || visited[newR][newC] || + forest[newR][newC] == '.') + continue; - //mark visited, aded to queue, then increment the time + // edge cases passed -> mark visited, add to queue, increment time visited[newR][newC] = true; - burningTree.offer(new int[] {newR, newC, time + 1}); + burningTree.offer(new int[] { newR, newC, time + 1 }); } } - return maxTime; + return maxStepCount; } } \ No newline at end of file diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..fb790c2 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -3,21 +3,68 @@ import org.junit.jupiter.api.Test; public class FireTest { + @Test public void testTimeToBurnExample() { 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', '.', 't' }, + { '.', '.', 't', 't', '.', '.', '.', '.', 't' }, + { '.', '.', 't', 't', 't', 't', 't', 't', 't' }, + { 't', 't', 't', 't', '.', '.', '.', '.', '.' } }; int matchR = 2; int matchC = 6; - int expected = 8; - int actual = Fire.timeToBurn(forest, matchR, matchC); + assertEquals(expected, Fire.timeToBurn(forest, matchR, matchC)); + } + + @Test + public void testSingleTree() { + char[][] forest = { + { 't' } + }; + assertEquals(0, Fire.timeToBurn(forest, 0, 0)); + } + + @Test + public void testNoBurnableTrees() { + char[][] forest = { + { '.', '.', '.' }, + { '.', '.', '.' }, + { '.', '.', '.' } + }; + assertEquals(0, Fire.timeToBurn(forest, 1, 1)); + } - assertEquals(expected, actual); + @Test + public void testAllConnected() { + char[][] forest = { + { 't', 't', 't' }, + { 't', 't', 't' }, + { 't', 't', 't' } + }; + int expected = 2; + assertEquals(expected, Fire.timeToBurn(forest, 1, 1)); + } + + @Test + public void testTreeInCorner() { + char[][] forest = { + { 't', 't', '.' }, + { 't', '.', '.' }, + { '.', '.', '.' } + }; + assertEquals(1, Fire.timeToBurn(forest, 0, 0)); + } + + @Test + public void testIsolatedTreeCluster() { + char[][] forest = { + { 't', '.', 't' }, + { '.', '.', '.' }, + { 't', 't', 't' } + }; + assertEquals(1, Fire.timeToBurn(forest, 2, 1)); } }