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
46 changes: 44 additions & 2 deletions src/Fire.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

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
Expand Down Expand Up @@ -35,9 +39,47 @@ 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
*/
public static int timeToBurn(char[][] forest, int matchR, int matchC) {
private static final int[][] directions = {
{-1, 0}, // LEFT
{ 1, 0}, // RIGHT
{ 0,-1}, // UP
{ 0, 1} // DOWN
};

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 rows = forest.length;
int cols = forest[0].length;

Queue<int[]> queue = new LinkedList<>();
boolean[][] visited = new boolean[rows][cols];

queue.add(new int[]{matchR, matchC, 0}); // Adding the starting cell with the time at 0
visited[matchR][matchC] = true; // Marking the first cell visited

int finalTime = 0; // keep track of the time

while(!queue.isEmpty()) {
int[] current = queue.poll(); // creating a pointer
int r = current[0];
int c = current[1];
int time = current[2]; // We set a third variable when dealing with time/steps

finalTime = Math.max(finalTime, time); // finding the max amount of time

for(int[] dir : directions) {
int newR = r + dir[0];
int newC = c + dir[1];

if(newR >= 0 && newR < rows &&
newC >= 0 && newC < cols &&
!visited[newR][newC] && forest[newR][newC] == 't') {
visited[newR][newC] = true;
queue.add(new int[]{newR, newC, time + 1}); // adding this neighbor to the queue with time + 1
}
}
}
return finalTime;
}
}
104 changes: 104 additions & 0 deletions src/FireTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,108 @@ public void testTimeToBurnExample() {

assertEquals(expected, actual);
}

@Test
public void testTimeToBurn_emptyForest() {
char[][] forest = {
{'.','.','.','.','.'},
{'.','.','.','.','.'},
{'.','.','.','.','.'},
{'.','.','.','.','.'}
};

int expected = 0;
int actual = Fire.timeToBurn(forest, 1, 1);
assertEquals(expected, actual);
}

@Test
public void testTimeToBurn_allTrees() {
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'}
};

int expected = 5;
int actual = Fire.timeToBurn(forest, 3, 2);
assertEquals(expected, actual);
}

@Test
public void testTimeToBurn_twoColumnsWithTrees() {
char[][] forest = {
{'t','t','.','.','.'},
{'t','t','.','.','.'},
{'t','t','.','.','.'},
{'t','t','.','.','.'},
{'t','t','.','.','.'}
};

int expected = 3;
int actual = Fire.timeToBurn(forest, 2, 0);
assertEquals(expected, actual);
}

@Test
public void testTimeToBurn_twoColumnsWithTreesButStartMatchInWhiteSpace() {
char[][] forest = {
{'t','t','.','.','.'},
{'t','t','.','.','.'},
{'t','t','.','.','.'},
{'t','t','.','.','.'},
{'t','t','.','.','.'}
};

int expected = 0;
int actual = Fire.timeToBurn(forest, 2, 3);
assertEquals(expected, actual);
}

@Test
public void testTimeToBurn_diagonalTreesStartingAtTopLeft() {
char[][] forest = {
{'t','.','.','.','.'},
{'.','t','.','.','.'},
{'.','.','t','.','.'},
{'.','.','.','t','.'},
{'.','.','.','.','t'}
};

int expected = 0;
int actual = Fire.timeToBurn(forest, 0, 0);
assertEquals(expected, actual);
}

@Test
public void testTimeToBurn_treesInTheCenter() {
char[][] forest = {
{'.','.','.','.','.'},
{'.','.','t','.','.'},
{'.','t','t','t','.'},
{'.','.','t','.','.'},
{'.','.','.','.','.'},
};

int expected = 1;
int actual = Fire.timeToBurn(forest, 2, 2);
assertEquals(expected, actual);
}

@Test
public void testTimeToBurn_spiralForest() {
char[][] forest = {
{'t','t','t','t','t'},
{'.','.','.','.','t'},
{'.','t','t','.','t'},
{'.','t','.','.','t'},
{'.','t','t','t','t'},
};

int expected = 14;
int actual = Fire.timeToBurn(forest, 0, 0);
assertEquals(expected, actual);
}
}