From 21bd1e922c6f11ca5a6484e4bd0f25f0848dfdb5 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:13:21 -0700 Subject: [PATCH 1/6] updated to include 0 as valid row/col index --- src/Fire.java | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index b968c51..68bc5df 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,6 +43,54 @@ 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? - return -1; + boolean[][] visited = new boolean[forest.length][forest[matchR].length]; + int time = 0; + Queue queue = new LinkedList<>(); + int[] start = {matchR, matchC}; + queue.add(start); + + while (!queue.isEmpty()) { + int rounds = queue.size(); + for (int i = 0; i < rounds; i++) { + int[] current = queue.poll(); + List nearby = findAdjacentTrees(forest, visited, current); + queue.addAll(nearby); + } + time++; + System.out.println(time); + System.out.println(queue); + } + + return time; + } + + public static List findAdjacentTrees(char[][] forest, boolean[][] visited, int[] current) { + List adjacentTrees = new ArrayList<>(); + int row = current[0]; + int col = current[1]; + + + int[][] directions = { + {1, 0}, + {-1, 0}, + {0, -1}, + {0, 1} + }; + + for (int[] direction : directions) { + int newR = row + direction[0]; + int newC = col + direction[1]; + + if (newR > 0 || newR < forest.length || + newC > 0 || newC < forest[row].length || + visited[newR][newC] == false || + forest[newR][newC] == 't') { + int[] tree = {newR, newC}; + adjacentTrees.add(tree); + visited[newR][newC] = true; + }; + }; + + return adjacentTrees; } } \ No newline at end of file From d1fe233cdd3aa0fccf233bad5de09dead749845c Mon Sep 17 00:00:00 2001 From: VibeSyntax <138536097+beza102@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:35:01 -0700 Subject: [PATCH 2/6] fixed a few issues --- src/Fire.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 68bc5df..1d8edc5 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -56,7 +56,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { List nearby = findAdjacentTrees(forest, visited, current); queue.addAll(nearby); } - time++; + if(!queue.isEmpty()) time++; System.out.println(time); System.out.println(queue); } @@ -81,9 +81,9 @@ public static List findAdjacentTrees(char[][] forest, boolean[][] visited int newR = row + direction[0]; int newC = col + direction[1]; - if (newR > 0 || newR < forest.length || - newC > 0 || newC < forest[row].length || - visited[newR][newC] == false || + if (newR >= 0 && newR < forest.length && + newC >= 0 && newC < forest[row].length && + visited[newR][newC] == false && forest[newR][newC] == 't') { int[] tree = {newR, newC}; adjacentTrees.add(tree); From 711850166943604daf29440a17cd2410293be279 Mon Sep 17 00:00:00 2001 From: VibeSyntax <138536097+beza102@users.noreply.github.com> Date: Fri, 6 Jun 2025 17:10:10 -0700 Subject: [PATCH 3/6] made a few change and added tests --- src/Fire.java | 15 +++++++++----- src/FireTest.java | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 1d8edc5..e42b5e9 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -46,8 +46,12 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { boolean[][] visited = new boolean[forest.length][forest[matchR].length]; int time = 0; Queue queue = new LinkedList<>(); - int[] start = {matchR, matchC}; - queue.add(start); + + if(forest[matchR][matchC]!= 't'){ + return 0; + } + visited[matchR][matchC] = true; + queue.add(new int[] { matchR, matchC}); while (!queue.isEmpty()) { int rounds = queue.size(); @@ -68,6 +72,7 @@ public static List findAdjacentTrees(char[][] forest, boolean[][] visited List adjacentTrees = new ArrayList<>(); int row = current[0]; int col = current[1]; + int[][] directions = { @@ -82,12 +87,12 @@ public static List findAdjacentTrees(char[][] forest, boolean[][] visited int newC = col + direction[1]; if (newR >= 0 && newR < forest.length && - newC >= 0 && newC < forest[row].length && + newC >= 0 && newC < forest[0].length && visited[newR][newC] == false && forest[newR][newC] == 't') { - int[] tree = {newR, newC}; - adjacentTrees.add(tree); visited[newR][newC] = true; + adjacentTrees.add(new int[] { newR, newC }); + }; }; diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..d1406b7 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,57 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testSingleTree() { + char[][] forest = { + {'.', '.', '.'}, + {'.', 't', '.'}, + {'.', '.', '.'} + }; + int matchR = 1; + int matchC = 1; + + //one tree burn + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void testNoTree(){ + char[][] forest = { + {'.', '.', '.'}, + {'.', '.', '.'}, + {'.', '.', '.'} + }; + int matchR = 1; + int matchC = 1; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + + } + + @Test + public void testAllTreesConnected() { + char[][] forest = { + {'t', 't', 't'}, + {'t', 't', 't'}, + {'t', 't', 't'} + }; + int matchR = 1; + int matchC = 1; + + int expected = 2; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + + } From 9ea93c4d4c76900677a111e439558ffc8ce77cab Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Sun, 8 Jun 2025 23:21:20 -0700 Subject: [PATCH 4/6] added test with smaller map --- src/Fire.java | 7 +++--- src/FireTest.java | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 1d8edc5..8dda2f2 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -48,6 +48,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { Queue queue = new LinkedList<>(); int[] start = {matchR, matchC}; queue.add(start); + visited[matchR][matchC] = true; while (!queue.isEmpty()) { int rounds = queue.size(); @@ -57,8 +58,8 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { queue.addAll(nearby); } if(!queue.isEmpty()) time++; - System.out.println(time); - System.out.println(queue); + // System.out.println(time); + // System.out.println(queue); } return time; @@ -85,7 +86,7 @@ public static List findAdjacentTrees(char[][] forest, boolean[][] visited newC >= 0 && newC < forest[row].length && visited[newR][newC] == false && forest[newR][newC] == 't') { - int[] tree = {newR, newC}; + int[] tree = {newR, newC }; adjacentTrees.add(tree); visited[newR][newC] = true; }; diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..2a0a5dd 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,58 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void adjacentTrees() { + char[][] forest = { + {'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, actual); + } + + @Test + public void adjacentTrees_OneTree() { + char[][] forest = { + {'t','.'}, + {'.','.'}, + {'.','.'}, + {'t','t'} + }; + + int matchR = 0; + int matchC = 0; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void adjacentTrees_TwoTrees() { + char[][] forest = { + {'t','.'}, + {'.','.'}, + {'.','.'}, + {'t','t'} + }; + + int matchR = 3; + int matchC = 1; + + int expected = 1; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } } From bcf8480ef5311f9e3238f11f1acf04ae6a818197 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Sun, 8 Jun 2025 23:52:11 -0700 Subject: [PATCH 5/6] added depth/time component to trees --- src/Fire.java | 17 +++++++++----- src/FireTest.java | 60 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 104de2e..2811752 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -44,33 +44,38 @@ 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? boolean[][] visited = new boolean[forest.length][forest[matchR].length]; - int time = 0; + int depth = 0; Queue queue = new LinkedList<>(); if(forest[matchR][matchC] != 't'){ return 0; } visited[matchR][matchC] = true; - queue.add(new int[] {matchR, matchC}); + queue.add(new int[] {matchR, matchC, depth}); while (!queue.isEmpty()) { int rounds = queue.size(); for (int i = 0; i < rounds; i++) { int[] current = queue.poll(); + depth = Math.max(current[2], depth); + List nearby = findAdjacentTrees(forest, visited, current); + queue.addAll(nearby); } - if(!queue.isEmpty()) time++; + + } - return time; + return depth; } public static List findAdjacentTrees(char[][] forest, boolean[][] visited, int[] current) { List adjacentTrees = new ArrayList<>(); int row = current[0]; int col = current[1]; - + int depth = current[2]; + int[][] directions = { {1, 0}, {-1, 0}, @@ -87,7 +92,7 @@ public static List findAdjacentTrees(char[][] forest, boolean[][] visited visited[newR][newC] == false && forest[newR][newC] == 't') { visited[newR][newC] = true; - adjacentTrees.add(new int[] { newR, newC }); + adjacentTrees.add(new int[] { newR, newC, depth + 1 }); }; }; diff --git a/src/FireTest.java b/src/FireTest.java index a513fb4..c956e25 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -1,5 +1,9 @@ +import static org.junit.Assert.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.ArrayList; +import java.util.List; + import org.junit.jupiter.api.Test; public class FireTest { @@ -93,7 +97,7 @@ public void adjacentTrees() { } @Test - public void adjacentTrees_OneTree() { + public void timeToBurn_OneTree() { char[][] forest = { {'t','.'}, {'.','.'}, @@ -111,7 +115,7 @@ public void adjacentTrees_OneTree() { } @Test - public void adjacentTrees_TwoTrees() { + public void timeToBurn_TwoTrees() { char[][] forest = { {'t','.'}, {'.','.'}, @@ -127,4 +131,56 @@ public void adjacentTrees_TwoTrees() { assertEquals(expected, actual); } + + @Test + public void adjacentTrees_TwoTrees() { + char[][] forest = { + {'t','.'}, + {'.','.'}, + {'.','.'}, + {'t','t'} + }; + + boolean[][] visited = new boolean[forest.length][forest[0].length]; + + + List expected = new ArrayList<>(); + expected.add(new int[]{3, 0, 1}); + + int matchR = 3; + int matchC = 1; + int depth = 0; + + int[] start = new int[] {matchR, matchC, depth}; + visited[matchR][matchC] = true; + + List nearby = Fire.findAdjacentTrees(forest, visited, start); + + for (int i = 0; i < expected.size(); i++) { + assertArrayEquals(expected.get(i), nearby.get(i)); + } + } + + @Test + public void adjacentTrees_NoneAdjacent() { + char[][] forest = { + {'t','.'}, + {'.','.'}, + {'.','.'}, + {'t','t'} + }; + + boolean[][] visited = new boolean[forest.length][forest[0].length]; + + int matchR = 0; + int matchC = 0; + int depth = 0; + + int[] start = new int[] {matchR, matchC, depth}; + visited[matchR][matchC] = true; + + List nearby = Fire.findAdjacentTrees(forest, visited, start); + + assertEquals(0, nearby.size()); + } } From 6a7bed6c1682cc2ffffd75642eb06dd239ebe9f1 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Mon, 9 Jun 2025 00:05:31 -0700 Subject: [PATCH 6/6] took out unnecessary loop and clarified variable name --- src/Fire.java | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 2811752..8cac4bb 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -44,30 +44,26 @@ 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? boolean[][] visited = new boolean[forest.length][forest[matchR].length]; - int depth = 0; + int maxDepth = 0; Queue queue = new LinkedList<>(); if(forest[matchR][matchC] != 't'){ return 0; } visited[matchR][matchC] = true; - queue.add(new int[] {matchR, matchC, depth}); + queue.add(new int[] {matchR, matchC, maxDepth}); while (!queue.isEmpty()) { - int rounds = queue.size(); - for (int i = 0; i < rounds; i++) { - int[] current = queue.poll(); - depth = Math.max(current[2], depth); - - List nearby = findAdjacentTrees(forest, visited, current); - - queue.addAll(nearby); - } - + int[] current = queue.poll(); + // Math.max shouldn't be necessary but adding it just in case + maxDepth = Math.max(maxDepth, current[2]); + + List nearby = findAdjacentTrees(forest, visited, current); + queue.addAll(nearby); } - return depth; + return maxDepth; } public static List findAdjacentTrees(char[][] forest, boolean[][] visited, int[] current) { @@ -75,7 +71,7 @@ public static List findAdjacentTrees(char[][] forest, boolean[][] visited int row = current[0]; int col = current[1]; int depth = current[2]; - + int[][] directions = { {1, 0}, {-1, 0},