From 3b2588a0e11ca13e80b90b75ed3f232010df35cb Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Tue, 22 Apr 2025 14:25:41 -0700 Subject: [PATCH 1/9] started logic without reference --- src/Fire.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..33e96c4 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? + Queue lit = new LinkedList<>(); + boolean[][] visited = new boolean[forest.length][forest[0].length]; + int time = 0; + int[][] moves = { + {-1, 0}, + {1, 0}, + {0, -1}, + {0, 1} + }; + + + + + return -1; } + + public static void traverseFire(char[][] forest, int matchR, int matchC, int[][] moves, boolean[][] visited){ + if (matchR < 0 || matchC < 0 || matchR >= forest.length || matchC >= forest[0].length){ + + } + + int[] coord = {matchR, matchC, time}; + } } \ No newline at end of file From b4d33b7a39f560f4f42ee023664aa5a2b83f0d91 Mon Sep 17 00:00:00 2001 From: Diana Date: Tue, 22 Apr 2025 14:39:33 -0700 Subject: [PATCH 2/9] Built a helper method --- src/Fire.java | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 33e96c4..dec9726 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 { @@ -41,28 +43,40 @@ 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[] start = {matchR, matchC}; Queue lit = new LinkedList<>(); + lit.add(start); + boolean[][] visited = new boolean[forest.length][forest[0].length]; int time = 0; - int[][] moves = { + + + return -1; + } + + public static List traverseFire(char[][] forest, int[] start, boolean[][] visited){ + List moves = new ArrayList<>(); + int[][] steps = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; + int curR = start[0]; + int curC = start[1]; + for (int[] step : steps) { + int newR = curR + step[0]; + int newC = curC + step[1]; - - - return -1; - } - - public static void traverseFire(char[][] forest, int matchR, int matchC, int[][] moves, boolean[][] visited){ - if (matchR < 0 || matchC < 0 || matchR >= forest.length || matchC >= forest[0].length){ - + if (newR > 0 && newR < forest.length && + newC >= 0 && newC < forest[0].length && + forest[newR][newC] != '.') { + moves.add(new int[]{newR, newC}); + } } - - int[] coord = {matchR, matchC, time}; + + return moves; } } \ No newline at end of file From 1d2d9dae6e9c193b2394bb077e4307bedfa663f6 Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Tue, 22 Apr 2025 15:06:00 -0700 Subject: [PATCH 3/9] finished Fire functionality --- src/Fire.java | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index dec9726..732abfb 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -43,18 +43,41 @@ 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[] start = {matchR, matchC}; + int[] start = {matchR, matchC, 0}; Queue lit = new LinkedList<>(); lit.add(start); boolean[][] visited = new boolean[forest.length][forest[0].length]; int time = 0; + int maxTime = 0; + + while (!lit.isEmpty()){ + int[] current = lit.poll(); + int curR = current[0]; + int curC = current[1]; + int currentTime = current[2]; + + if (currentTime > maxTime){ + maxTime = currentTime; + } + + if (visited[curR][curC]){ + continue; + } + + visited[curR][curC] = true; + + List nextMoves = traverseFire(forest, current, currentTime); + + lit.addAll(nextMoves); + + } - return -1; + return maxTime; } - public static List traverseFire(char[][] forest, int[] start, boolean[][] visited){ + public static List traverseFire(char[][] forest, int[] current, int time){ List moves = new ArrayList<>(); int[][] steps = { {-1, 0}, @@ -63,8 +86,9 @@ public static List traverseFire(char[][] forest, int[] start, boolean[][] {0, 1} }; - int curR = start[0]; - int curC = start[1]; + int curR = current[0]; + int curC = current[1]; + time++; for (int[] step : steps) { int newR = curR + step[0]; @@ -73,7 +97,7 @@ public static List traverseFire(char[][] forest, int[] start, boolean[][] if (newR > 0 && newR < forest.length && newC >= 0 && newC < forest[0].length && forest[newR][newC] != '.') { - moves.add(new int[]{newR, newC}); + moves.add(new int[]{newR, newC, time}); } } From cdf3ba9b258ca901340016496ee491fab3af3152 Mon Sep 17 00:00:00 2001 From: Diana Date: Tue, 22 Apr 2025 15:17:09 -0700 Subject: [PATCH 4/9] Added JUnit tests --- src/Fire.java | 4 --- src/FireTest.java | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 732abfb..0ee0f2d 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -48,7 +48,6 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { lit.add(start); boolean[][] visited = new boolean[forest.length][forest[0].length]; - int time = 0; int maxTime = 0; while (!lit.isEmpty()){ @@ -66,14 +65,11 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { } visited[curR][curC] = true; - List nextMoves = traverseFire(forest, current, currentTime); lit.addAll(nextMoves); - } - return maxTime; } diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..1f81f2d 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,71 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testTimeToBurnEmptySurroundedTree() { + char[][] forest = { + {'.','.','.','.'}, + {'.','.','.','t'}, + {'.','.','.','.'}, + {'.','.','.','.'} + }; + + int matchR = 1; + int matchC = 3; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurnOneTree() { + char[][] forest = { + {'t'} + }; + + int matchR = 0; + int matchC = 0; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurnDiagonalTrees() { + char[][] forest = { + {'t','.','t'}, + {'.','t','.'}, + {'t','.','t'} + }; + + int matchR = 1; + int matchC = 1; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurnAllTrees() { + char[][] forest = { + {'t','t','t'}, + {'t','t','t'}, + {'t','t','t'} + }; + + int matchR = 1; + int matchC = 1; + + int expected = 3; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } } From 64f58881553f497a897783bedbe52e9fd65a4661 Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Tue, 22 Apr 2025 15:20:18 -0700 Subject: [PATCH 5/9] fixed conditional --- src/Fire.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 0ee0f2d..4495239 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -90,9 +90,9 @@ public static List traverseFire(char[][] forest, int[] current, int time) int newR = curR + step[0]; int newC = curC + step[1]; - if (newR > 0 && newR < forest.length && + if (newR >= 0 && newR < forest.length && newC >= 0 && newC < forest[0].length && - forest[newR][newC] != '.') { + forest[newR][newC] != 't') { moves.add(new int[]{newR, newC, time}); } } From 9d999f5c73a3b8eafd790fdba2413398a0cd4909 Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Tue, 22 Apr 2025 15:32:40 -0700 Subject: [PATCH 6/9] trying to fix junit tests --- src/Fire.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 4495239..2dacd06 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -55,7 +55,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { int curR = current[0]; int curC = current[1]; int currentTime = current[2]; - + if (currentTime > maxTime){ maxTime = currentTime; } @@ -65,11 +65,14 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { } visited[curR][curC] = true; + List nextMoves = traverseFire(forest, current, currentTime); lit.addAll(nextMoves); + } + return maxTime; } @@ -84,7 +87,7 @@ public static List traverseFire(char[][] forest, int[] current, int time) int curR = current[0]; int curC = current[1]; - time++; + for (int[] step : steps) { int newR = curR + step[0]; @@ -92,8 +95,8 @@ public static List traverseFire(char[][] forest, int[] current, int time) if (newR >= 0 && newR < forest.length && newC >= 0 && newC < forest[0].length && - forest[newR][newC] != 't') { - moves.add(new int[]{newR, newC, time}); + forest[newR][newC] != '.') { + moves.add(new int[]{newR, newC, time + 1}); } } From 2dc4fd53bbeaee1a6c91b43b32db9f86f1d0372a Mon Sep 17 00:00:00 2001 From: Diana Date: Tue, 22 Apr 2025 15:35:26 -0700 Subject: [PATCH 7/9] Fixing.. --- src/Fire.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 2dacd06..a920e54 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -95,7 +95,7 @@ public static List traverseFire(char[][] forest, int[] current, int time) if (newR >= 0 && newR < forest.length && newC >= 0 && newC < forest[0].length && - forest[newR][newC] != '.') { + forest[newR][newC] == 't') { moves.add(new int[]{newR, newC, time + 1}); } } From 92a5e66163b76843a70e91b990f0feb8bd95718e Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Tue, 22 Apr 2025 15:44:23 -0700 Subject: [PATCH 8/9] added new test --- src/Fire.java | 4 +++- src/FireTest.java | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index a920e54..a2243f5 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -68,6 +68,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { List nextMoves = traverseFire(forest, current, currentTime); + lit.addAll(nextMoves); } @@ -87,6 +88,7 @@ public static List traverseFire(char[][] forest, int[] current, int time) int curR = current[0]; int curC = current[1]; + time++; for (int[] step : steps) { @@ -96,7 +98,7 @@ public static List traverseFire(char[][] forest, int[] current, int time) if (newR >= 0 && newR < forest.length && newC >= 0 && newC < forest[0].length && forest[newR][newC] == 't') { - moves.add(new int[]{newR, newC, time + 1}); + moves.add(new int[]{newR, newC, time}); } } diff --git a/src/FireTest.java b/src/FireTest.java index 1f81f2d..0bf495b 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -21,6 +21,24 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + @Test + public void testTimeToBurnSimpler() { + char[][] forest = { + {'t','.','.','t'}, + {'.','.','t','t'}, + {'t','.','t','t'}, + {'t','t','t','t'} + }; + + int matchR = 2; + int matchC = 2; + + int expected = 4; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + @Test public void testTimeToBurnEmptySurroundedTree() { char[][] forest = { From 614595e44ae3acdb9147a28d4d8f5dfee7084441 Mon Sep 17 00:00:00 2001 From: Diana Date: Tue, 22 Apr 2025 15:48:16 -0700 Subject: [PATCH 9/9] Modified code and fixed the issues for JUnit tests --- src/Fire.java | 22 +++++++++------------- src/FireTest.java | 2 +- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index a2243f5..75313e9 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -52,28 +52,26 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { while (!lit.isEmpty()){ int[] current = lit.poll(); - int curR = current[0]; - int curC = current[1]; int currentTime = current[2]; if (currentTime > maxTime){ maxTime = currentTime; } - if (visited[curR][curC]){ - continue; - } - - visited[curR][curC] = true; - List nextMoves = traverseFire(forest, current, currentTime); + for (int[] move : nextMoves) { + int newR = move[0]; + int newC = move[1]; - lit.addAll(nextMoves); + if (!visited[newR][newC]) { + visited[newR][newC] = true; + lit.add(new int[]{newR, newC, currentTime + 1}); + } + } } - return maxTime; } @@ -88,9 +86,7 @@ public static List traverseFire(char[][] forest, int[] current, int time) int curR = current[0]; int curC = current[1]; - time++; - - + for (int[] step : steps) { int newR = curR + step[0]; int newC = curC + step[1]; diff --git a/src/FireTest.java b/src/FireTest.java index 0bf495b..740404f 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -100,7 +100,7 @@ public void testTimeToBurnAllTrees() { int matchR = 1; int matchC = 1; - int expected = 3; + int expected = 2; int actual = Fire.timeToBurn(forest, matchR, matchC); assertEquals(expected, actual);