Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion src/Fire.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<int[]>
Queue<int[]> 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<int[]> moves = possibleMoves(forest, current, curTimer + 1);
treeFire.addAll(moves);
timer = Math.max(curTimer, timer);
} else {
continue;
}
}
return timer;
}

private static List<int[]> possibleMoves(char[][] forest, int[] currentLocation, int timer) {
int curR = currentLocation[0];
int curC = currentLocation[1];

List<int[]> 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;
}
}
57 changes: 57 additions & 0 deletions src/FireTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
Expand All @@ -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());
}

}