From 625046b2489924d97b1e7ed8d5ed64c26632af07 Mon Sep 17 00:00:00 2001 From: Jameson Farmer Date: Tue, 22 Apr 2025 14:17:35 -0700 Subject: [PATCH 1/8] Method started --- src/Fire.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..ae47aba 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,18 @@ 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? + //neighbors method + //we need visisted + //pass int[] in queue like: row, column, time + + int[] start = new int[]{matchR, matchC, 0}; + boolean[][] burnt = new boolean[forest.length][forest[0].length]; + Queue queue = new LinkedList<>(); + queue.add(start); + + while(!queue.isEmpty()){ + + } return -1; } } \ No newline at end of file From 7e8f906d41317c719884dea6df3aea4f5a2ed162 Mon Sep 17 00:00:00 2001 From: Jameson Farmer Date: Tue, 22 Apr 2025 14:23:05 -0700 Subject: [PATCH 2/8] More logic added --- src/Fire.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index ae47aba..c00272f 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -51,6 +51,18 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { queue.add(start); while(!queue.isEmpty()){ + int[] current = queue.poll(); + + int row = current[0]; + int col = current[1]; + int time = current[2]; + + if(burnt[row][col]){ + continue; + } + + burnt[row][col] = true; + } return -1; From 73519b65519ad68937e1210817d1039436456f1f Mon Sep 17 00:00:00 2001 From: alstondsouza1 Date: Tue, 22 Apr 2025 14:31:06 -0700 Subject: [PATCH 3/8] added possible method method --- src/Fire.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index c00272f..66e7899 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 { @@ -67,4 +69,25 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { } return -1; } + + 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[] direction : directions) { + int newRow = row + direction[0]; + int newCol = col + direction[1]; + + if (newRow >= 0 && newRow < forest.length && newCol >= 0 && newCol < forest[0].length && forest[newRow][newCol] == 't') { + moves.add(new int[]{newRow, newCol}); + } + } + return moves; + } } \ No newline at end of file From 825704215e71e4ae8a4e30cca51959788349929f Mon Sep 17 00:00:00 2001 From: Jameson Farmer Date: Tue, 22 Apr 2025 14:44:20 -0700 Subject: [PATCH 4/8] Completed! --- src/Fire.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 66e7899..1eac7fe 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -51,23 +51,30 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { 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]; + int time = current[2]; if(burnt[row][col]){ continue; } - burnt[row][col] = true; + List moves = possibleMoves(forest, row, col); + for(var move : moves){ + int[] toAdd = new int[]{move[0],move[1], time + 1}; + queue.add(toAdd); + } + if(time > maxTime){ + maxTime = time; + } } - return -1; + return maxTime; } public static List possibleMoves(char[][] forest, int row, int col) { From b8bf51e97d403e81a898b215ace4b31edf41b09b Mon Sep 17 00:00:00 2001 From: Jameson Farmer Date: Tue, 22 Apr 2025 14:50:56 -0700 Subject: [PATCH 5/8] Added no burn test --- src/FireTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..7cd1392 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,23 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testNoburn() { + char[][] forest = { + {'.','.','.'}, + {'.','t','.'}, + {'.','.','.'} + + }; + + int matchR = 2; + int matchC = 2; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } } + From ce2f6a3f908052e0975b4bb1f615dc1f3528a9f6 Mon Sep 17 00:00:00 2001 From: alstondsouza1 Date: Tue, 22 Apr 2025 14:56:16 -0700 Subject: [PATCH 6/8] added more testcases --- src/FireTest.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index 7cd1392..916facc 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -38,5 +38,31 @@ public void testNoburn() { assertEquals(expected, actual); } + + @Test + public void testFullForest() { + char[][] forest = { + {'t', 't', 't'}, + {'t', 't', 't'}, + {'t', 't', 't'} + }; + int matchR = 1, matchC = 1; + int expected = 2; + int actual = Fire.timeToBurn(forest, matchR, matchC); + assertEquals(expected, actual); + } + + @Test + public void testEmptyForest() { + char[][] forest = { + {'.', '.', '.'}, + {'.', '.', '.'}, + {'.', '.', '.'} + }; + int matchR = 1, matchC = 1; + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + assertEquals(expected, actual); + } } From ac9961bda052ebd0ae3dd0b5a310281d378c65a9 Mon Sep 17 00:00:00 2001 From: alstondsouza1 Date: Tue, 22 Apr 2025 23:58:12 -0700 Subject: [PATCH 7/8] added comments and add more testcases --- src/Fire.java | 30 +++++++++++++++++++++++------- src/FireTest.java | 26 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 1eac7fe..db36a2a 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -43,15 +43,22 @@ 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? - //neighbors method - //we need visisted - //pass int[] in queue like: row, column, time + // neighbors method + // we need visisted + // pass int[] in queue like: row, column, time + // create starting point int[] start = new int[]{matchR, matchC, 0}; + + // create a queue to hold the burning trees boolean[][] burnt = new boolean[forest.length][forest[0].length]; + Queue queue = new LinkedList<>(); queue.add(start); + int maxTime = 0; + + // process all points in the queue while(!queue.isEmpty()){ int[] current = queue.poll(); @@ -59,24 +66,29 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { int col = current[1]; int time = current[2]; - if(burnt[row][col]){ + // check if the current tree is already burnt + if (burnt[row][col]){ continue; } + burnt[row][col] = true; List moves = possibleMoves(forest, row, col); - for(var move : moves){ + + // add each 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){ + if( time > maxTime){ maxTime = time; } } return maxTime; } + // returns a list of valid moves for a tree at the given location public static List possibleMoves(char[][] forest, int row, int col) { List moves = new ArrayList<>(); @@ -87,14 +99,18 @@ public static List possibleMoves(char[][] forest, int row, int col) { {0, 1} // right }; + // check all four directions for (int[] direction : directions) { int newRow = row + direction[0]; int newCol = col + direction[1]; - if (newRow >= 0 && newRow < forest.length && newCol >= 0 && newCol < forest[0].length && forest[newRow][newCol] == 't') { + if (newRow >= 0 && newRow < forest.length && + newCol >= 0 && newCol < forest[0].length && + forest[newRow][newCol] == 't') { moves.add(new int[]{newRow, newCol}); } } + return moves; } } \ No newline at end of file diff --git a/src/FireTest.java b/src/FireTest.java index 916facc..cf39d0d 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -64,5 +64,31 @@ public void testEmptyForest() { int actual = Fire.timeToBurn(forest, matchR, matchC); assertEquals(expected, actual); } + + @Test + public void testIsolatedTree() { + char[][] forest = { + {'.', '.', '.'}, + {'.', 't', '.'}, + {'.', '.', '.'} + }; + int matchR = 1, matchC = 1; + int expected = 0; // no other trees to burn + int actual = Fire.timeToBurn(forest, matchR, matchC); + assertEquals(expected, actual); + } + + @Test + public void testCornerTreeSpread() { + char[][] forest = { + {'t', '.', '.'}, + {'.', 't', '.'}, + {'.', '.', 't'} + }; + int matchR = 0, matchC = 0; + int expected = 0; // fire can't spread diagonally + int actual = Fire.timeToBurn(forest, matchR, matchC); + assertEquals(expected, actual); + } } From 1304513d2b2e68fdb5ff77e8167ef168afe6257d Mon Sep 17 00:00:00 2001 From: Jameson Farmer Date: Wed, 23 Apr 2025 14:50:00 -0700 Subject: [PATCH 8/8] Tests refined --- src/FireTest.java | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/FireTest.java b/src/FireTest.java index cf39d0d..d3cf498 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -30,8 +30,8 @@ public void testNoburn() { }; - int matchR = 2; - int matchC = 2; + int matchR = 1; + int matchC = 1 ; int expected = 0; int actual = Fire.timeToBurn(forest, matchR, matchC); @@ -65,19 +65,6 @@ public void testEmptyForest() { assertEquals(expected, actual); } - @Test - public void testIsolatedTree() { - char[][] forest = { - {'.', '.', '.'}, - {'.', 't', '.'}, - {'.', '.', '.'} - }; - int matchR = 1, matchC = 1; - int expected = 0; // no other trees to burn - int actual = Fire.timeToBurn(forest, matchR, matchC); - assertEquals(expected, actual); - } - @Test public void testCornerTreeSpread() { char[][] forest = {