From 535f913189d89e2875d5801ef8d835bbac1a5b53 Mon Sep 17 00:00:00 2001 From: rjfredr Date: Thu, 5 Jun 2025 10:53:07 -0700 Subject: [PATCH 1/4] possible moves --- src/Fire.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Fire.java b/src/Fire.java index b968c51..359a8fb 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,6 @@ +import java.util.ArrayList; +import java.util.List; + public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a @@ -40,4 +43,20 @@ public static int timeToBurn(char[][] forest, int matchR, int matchC) { // just a location. What other information might be useful? return -1; } + public static List possibleMoves(char[][] forest, int[] curr) { + int[][] directions = {{-1,0},{1,0},{0,-1},{0,1}}; + List moves = new ArrayList<>(); + for (int[] dir : directions) { + int newROW = curr[0] + dir[0]; + int newCOL = curr[1] + dir[1]; + if (newROW>=0&& + newROW=0&& + newCOL Date: Thu, 5 Jun 2025 11:05:32 -0700 Subject: [PATCH 2/4] Start of timeToBurn method --- src/Fire.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Fire.java b/src/Fire.java index b968c51..ef523d7 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,24 @@ 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[][] onFire = new boolean[forest.length][forest[0].length]; + Queue queue = new LinkedList<>(); + int depth = 0; + queue.add(new int[]{matchR, matchC, depth}); + + while (!queue.isEmpty()) { + int[] current = queue.poll(); + int currR = current[0]; + int currC = current[1]; + + if(onFire[currR][currC]) { + continue; + } + + onFire[currR][currC] = true; + + queue.addAll(possibleMoves(forest, current), depth += 1); + } + return depth; } } \ No newline at end of file From 71a5899819894d1234c12a6446cee21ac7ae4a68 Mon Sep 17 00:00:00 2001 From: rjfredr Date: Thu, 5 Jun 2025 11:30:12 -0700 Subject: [PATCH 3/4] tests --- src/FireTest.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..dfdd366 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -18,6 +18,38 @@ public void testTimeToBurnExample() { int expected = 8; int actual = Fire.timeToBurn(forest, matchR, matchC); + assertEquals(expected, actual); + } + @Test + public void testTimeToBurnExample2() { + char[][] forest = { + {'t','.','.','t','t','t','t','.','t'}, + {'.','.','t','.','.','.','.','.','t'}, + {'.','.','t','t','t','t','t','t','t'}, + {'t','t','t','t','.','.','.','.','.'} + }; + + int matchR = 0; + int matchC = 3; + + int expected = 3; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + @Test + public void testTimeToBurnExample3() { + char[][] forest = { + {'t','.','t','t','t','t','t','.','t'}, + {'t','.','t','.','.','.','t','.','t'}, + }; + + int matchR = 0; + int matchC = 3; + + int expected = 4; + int actual = Fire.timeToBurn(forest, matchR, matchC); + assertEquals(expected, actual); } } From 12ed93f8c19a229dadd87bc9964829d298cf2e51 Mon Sep 17 00:00:00 2001 From: RJ Date: Sun, 8 Jun 2025 15:23:40 -0700 Subject: [PATCH 4/4] added 4 new tests --- src/FireTest.java | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index dfdd366..43013ab 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -50,6 +50,92 @@ public void testTimeToBurnExample3() { int expected = 4; int actual = Fire.timeToBurn(forest, matchR, matchC); + assertEquals(expected, actual); + } + @Test + public void testTimeToBurnExample4() { + 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 = 3; + + int expected = 2; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + @Test + public void testTimeToBurnExample5() { + 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 = 3; + int matchC = 4; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + @Test + public void testTimeToBurnExample6() { + char[][] forest = { + {'t','t','t','t','t','t','t','t','t'} + }; + + int matchR = 0; + int matchC = 1; + + int expected = 7; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + @Test + public void testTimeToBurnExample7() { + 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'}, + {'t','t','t','t'}, + }; + + int matchR = 4; + int matchC = 1; + + int expected = 8; + int actual = Fire.timeToBurn(forest, matchR, matchC); + + assertEquals(expected, actual); + } + @Test + public void testTimeToBurnExample8() { + char[][] forest = { + {'t','t','t','.','.','.','t','t','t'} + }; + + int matchR = 0; + int matchC = 4; + + int expected = 0; + int actual = Fire.timeToBurn(forest, matchR, matchC); + assertEquals(expected, actual); } }