diff --git a/src/Fire.java b/src/Fire.java index b968c51..7e34ea4 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,80 @@ 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; + + if (forest[matchR][matchC] != 't') throw new IllegalArgumentException("Not a tree!"); + + 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, timer}); + + // 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(); + // 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; + List moves = possibleMoves(forest, current, curTimer + 1); + treeFire.addAll(moves); + timer = Math.max(curTimer, timer); + } else { + continue; + } + } + return timer; + } + + private static List possibleMoves(char[][] forest, int[] currentLocation, int timer) { + 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, timer}); + } + } + return moves; } } \ No newline at end of file diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..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; @@ -20,4 +21,60 @@ 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); + } + + @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()); + } + }