From 3ee292f3ca0dddfc741bc75ecc936c70668e53f0 Mon Sep 17 00:00:00 2001 From: f3liz Date: Tue, 22 Apr 2025 14:25:42 -0700 Subject: [PATCH 1/7] possibleMoves implemented? i hope --- src/Fire.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..fc59db3 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,6 @@ +import java.util.ArrayList; +import java.util.*; + public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a @@ -35,9 +38,45 @@ public class Fire { * @param matchC The column the match is lit at * @return the time at which the final tree to be incinerated starts burning */ + + // need possible moves + // logic to make sure next possible move is a tree that is also not on fire + // queue + // add possiblemoves + // traverse + // will need visited grid + // need count for time + // + + 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; } + + private List possibleMoves(char[][] forest, int[] currentLocation) { + int curR = currentLocation[0]; + int curC = currentLocation[1]; + + List moves = new ArrayList<>(); + + int[][] directions = new int[][] { + {-1, 0}, // up + {1, 0}, // down + {0, -1}, // left + {0, 1} // right + }; + + for(int[] direction: directions) { + int newR = curR + direction[0]; + int newC = curC + direction[1]; + + if(newR < 0 || newR >= forest.length || newC < 0 || newC >= forest[0].length || forest[newR][newC] != 't') { + moves.add(new int[]{newR, newC}); + } + } + + return moves; + } } \ No newline at end of file From 144ce7a805584ae57b00398462e34512eb40ac0e Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Tue, 22 Apr 2025 14:37:42 -0700 Subject: [PATCH 2/7] timetoburn main logic implemented --- src/Fire.java | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index fc59db3..477031e 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -52,10 +52,50 @@ 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; + + // int timer + int timer = 0; + // boolean[][] visited + boolean[][] visited = new boolean[forest.length][forest[0].length]; + + // queue linkedlist + Queue treeFire = new LinkedList<>(); + // add current location to queue + treeFire.add(new int[]{matchR, matchC}); + + // while queue is not empty, iterate + // poll + // if location valid + // add to visited + // change char to f + // add possible moves + // timer++; + + while (!treeFire.isEmpty()) { + int[] current = treeFire.poll(); + timer++; + // separate rows and colums + int curR = current[0]; + int curC = current[1]; + + if (!visited[curR][curC]) { + visited[curR][curC] = true; + forest[curR][curC] = 'b'; + List moves = possibleMoves(forest, current); + + treeFire.addAll(moves); + + } else { + continue; + } + } + + + + return timer; } - private List possibleMoves(char[][] forest, int[] currentLocation) { + private static List possibleMoves(char[][] forest, int[] currentLocation) { int curR = currentLocation[0]; int curC = currentLocation[1]; From 61286c6cafda6bca0d3b929d7b8cd1075a3b733d Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Tue, 22 Apr 2025 14:49:39 -0700 Subject: [PATCH 3/7] Added timer --- src/Fire.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 477031e..57dcfa4 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -53,15 +53,15 @@ 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 timer int timer = 0; + // boolean[][] visited boolean[][] visited = new boolean[forest.length][forest[0].length]; // queue linkedlist Queue treeFire = new LinkedList<>(); // add current location to queue - treeFire.add(new int[]{matchR, matchC}); + treeFire.add(new int[]{matchR, matchC, timer}); // while queue is not empty, iterate // poll @@ -73,18 +73,17 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { while (!treeFire.isEmpty()) { int[] current = treeFire.poll(); - timer++; // separate rows and colums int curR = current[0]; int curC = current[1]; + int curTimer = current[2]; + // if curR = -1, timer++ if (!visited[curR][curC]) { visited[curR][curC] = true; forest[curR][curC] = 'b'; - List moves = possibleMoves(forest, current); - + List moves = possibleMoves(forest, current, curTimer + 1); treeFire.addAll(moves); - } else { continue; } @@ -95,7 +94,7 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { return timer; } - private static List possibleMoves(char[][] forest, int[] currentLocation) { + private static List possibleMoves(char[][] forest, int[] currentLocation, int timer) { int curR = currentLocation[0]; int curC = currentLocation[1]; @@ -113,7 +112,7 @@ private static List possibleMoves(char[][] forest, int[] currentLocation) int newC = curC + direction[1]; if(newR < 0 || newR >= forest.length || newC < 0 || newC >= forest[0].length || forest[newR][newC] != 't') { - moves.add(new int[]{newR, newC}); + moves.add(new int[]{newR, newC, timer}); } } From 05804fd88ddf312e3697624a7e63dcf1daa3c8e9 Mon Sep 17 00:00:00 2001 From: f3liz Date: Tue, 22 Apr 2025 14:51:47 -0700 Subject: [PATCH 4/7] error fixes --- src/Fire.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 57dcfa4..ed1ce62 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -111,7 +111,7 @@ private static List possibleMoves(char[][] forest, int[] currentLocation, int newR = curR + direction[0]; int newC = curC + direction[1]; - if(newR < 0 || newR >= forest.length || newC < 0 || newC >= forest[0].length || forest[newR][newC] != 't') { + if(newR >= 0 && newR < forest.length && newC >= 0 && newC < forest[0].length && forest[newR][newC] == 't') { moves.add(new int[]{newR, newC, timer}); } } From 528eae0c34623590d2e6fc29211fc7268f6f977a Mon Sep 17 00:00:00 2001 From: f3liz Date: Tue, 22 Apr 2025 14:54:07 -0700 Subject: [PATCH 5/7] done, forest burnt down --- src/Fire.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index ed1ce62..45bcc0c 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -46,8 +46,6 @@ public class Fire { // traverse // will need visited grid // need count for time - // - public static int timeToBurn(char[][] forest, int matchR, int matchC) { // HINT: when adding to your BFS queue, you can include more information than @@ -84,13 +82,11 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { forest[curR][curC] = 'b'; List moves = possibleMoves(forest, current, curTimer + 1); treeFire.addAll(moves); + timer = Math.max(curTimer, timer); } else { continue; } } - - - return timer; } @@ -115,7 +111,6 @@ private static List possibleMoves(char[][] forest, int[] currentLocation, moves.add(new int[]{newR, newC, timer}); } } - return moves; } } \ No newline at end of file From 4e09450046ef83d4500cf84b81ed38b515873a2d Mon Sep 17 00:00:00 2001 From: f3liz Date: Tue, 22 Apr 2025 14:55:50 -0700 Subject: [PATCH 6/7] burnt 1 tree test --- src/FireTest.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..a7552b0 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -20,4 +20,22 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + @Test + public void testTimeToBurnExampleOneTree() { + char[][] forest = { + {'t','.','.','t','t','t','t','.','t'}, + {'.','.','t','t','.','.','.','.','t'}, + {'.','.','t','t','t','t','t','t','t'}, + {'t','t','t','t','.','.','.','.','.'} + }; + + int matchR = 0; + int matchC = 0; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } } From 203230a34d82312a3f8870535f7fb179af88f856 Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Tue, 22 Apr 2025 15:04:20 -0700 Subject: [PATCH 7/7] Added tests --- src/Fire.java | 3 ++- src/FireTest.java | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index 45bcc0c..7e34ea4 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -51,6 +51,8 @@ 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? + if (forest[matchR][matchC] != 't') throw new IllegalArgumentException("Not a tree!"); + int timer = 0; // boolean[][] visited @@ -79,7 +81,6 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { // if curR = -1, timer++ if (!visited[curR][curC]) { visited[curR][curC] = true; - forest[curR][curC] = 'b'; List moves = possibleMoves(forest, current, curTimer + 1); treeFire.addAll(moves); timer = Math.max(curTimer, timer); diff --git a/src/FireTest.java b/src/FireTest.java index a7552b0..26ce8cd 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -1,3 +1,4 @@ +import static org.junit.Assert.assertThrows; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; @@ -38,4 +39,42 @@ public void testTimeToBurnExampleOneTree() { assertEquals(expected, actual); } + + @Test + public void testTimeToBurnLongPath() { + char[][] forest = { + {'t','t','t','t','t','t','t','t','t'}, + {'t','.','.','.','.','.','.','.','.'}, + {'t','.','t','t','t','t','t','t','t'}, + {'t','t','t','.','.','.','.','.','.'} + }; + + int matchR = 0; + int matchC = 8; + + int expected = 20; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + + @Test + public void testTimeToBurnNoTree() { + char[][] forest = { + {'t','t','t','t','t','t','t','t','t'}, + {'t','.','.','.','.','.','.','.','.'}, + {'t','.','t','t','t','t','t','t','t'}, + {'t','t','t','.','.','.','.','.','.'} + }; + + int matchR = 1; + int matchC = 1; + + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + Fire.timeToBurn(forest, matchR, matchC); + }); + + assertEquals("Not a tree!", exception.getMessage()); + } + }