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
48 changes: 47 additions & 1 deletion src/Fire.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -38,6 +41,49 @@ 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 rows = forest.length;
int cols = forest[0].length;

boolean[][] visited = new boolean[rows][cols];
// BFS queue
Queue<int[]> queue = new LinkedList<>();
int[][] directions =
{{1,0},//Down
{-1,0}, //Up
{0,1}, //Right
{0,-1} //Left
};

// Start from the lit tree with time = 0
queue.add(new int[]{matchR, matchC, 0});
visited[matchR][matchC] = true;

int maxTime = 0;

while (!queue.isEmpty()) {
int[] current = queue.poll();
int r = current[0], c = current[1], time = current[2];
maxTime = Math.max(maxTime, time);

// Check all 4 directions
for (int[] d : directions) {
int newR = r + d[0];
int newC = c + d[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});
}
}
}

return maxTime;


}
}
81 changes: 81 additions & 0 deletions src/FireTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,85 @@ public void testTimeToBurnExample() {

assertEquals(expected, actual);
}

@Test
public void testSingleTree() {
char[][] forest = {
{'t'}
};
assertEquals(0, Fire.timeToBurn(forest, 0, 0));
}
@Test
public void testTreeSurroundedByGround() {
char[][] forest = {
{'.','.','.'},
{'.','t','.'},
{'.','.','.'}
};
assertEquals(0, Fire.timeToBurn(forest, 1, 1));
}

@Test
public void testNoConnectedTrees() {
char[][] forest = {
{'t','.','t'},
{'.','.','.'},
{'t','.','t'}
};
assertEquals(0, Fire.timeToBurn(forest, 0, 0));
}

@Test
public void testLineOfTrees() {
char[][] forest = {
{'t','t','t','t','t'}
};
assertEquals(4, Fire.timeToBurn(forest, 0, 0));
}




@Test
public void testEmptyForest() {
char[][] forest = {
{'.','.','.'},
{'.','.','.'},
{'.','.','.'}
};
assertEquals(0, Fire.timeToBurn(forest, 1, 1));
}

@Test
public void testFullConnectedBlock() {
char[][] forest = {
{'t','t'},
{'t','t'}
};
assertEquals(2, Fire.timeToBurn(forest, 0, 0));
}

@Test
public void testCornerUnreachableTrees() {
char[][] forest = {
{'t','.','.','t'},
{'.','.','.','.'},
{'t','.','.','t'}
};
assertEquals(0, Fire.timeToBurn(forest, 0, 0));
}

@Test
public void testVerticalColumn() {
char[][] forest = {
{'t'},
{'t'},
{'t'},
{'t'}
};
// Fire moves down each row
assertEquals(3, Fire.timeToBurn(forest, 0, 0));
}


}