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
82 changes: 81 additions & 1 deletion src/Fire.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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 @@ -38,6 +39,85 @@ 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;

//BFS
//int[] = [r ,c, time]
//return time

//checking if match lit is a tree, if no tree return 0 else do everything that follows
if(forest[matchR][matchC] == '.')
{
return 0;
}

boolean[][] visited = new boolean[forest.length][forest[0].length];
int[] initialBurnTime = {matchR, matchC, 0};

int[][] directions = {
{-1,0}, //up
{1,0}, //down
{0,-1}, //left
{0,1} //right
};

int burnTime = 0;

Queue<int[]> q = new LinkedList<>();
q.add(initialBurnTime);
int[] currBurningTree = new int[3];
while(!q.isEmpty())
{
currBurningTree = q.poll();
if(visited[currBurningTree[0]][currBurningTree[1]])
{
continue;
}

visited[currBurningTree[0]][currBurningTree[1]] = true;

if(forest[currBurningTree[0]][currBurningTree[1]] == 't')
{
burnTime = Math.max(burnTime, currBurningTree[2]);
}

//find other burning trees method call here
List<int[]> treeNeighbors = getTreeNeighbors(currBurningTree[0], currBurningTree[1], directions, forest, currBurningTree[2]);
q.addAll(treeNeighbors);
}
return burnTime;
}

//MORE HINT: current depth for third variable
//keep track of the depth for each movement
//return last depth element in the q for the burn time


public static List<int[]> getTreeNeighbors(int r, int c, int[][] directions, char[][] forest, int currTime)
{
List<int[]> possibleTrees = new ArrayList<>();

for(int[] d : directions)
{
int rr = r + d[0];
int cc = c + d[1];


if (rr >= 0 && cc >= 0 && rr < forest.length
&& cc < forest[0].length && forest[rr][cc] == 't') {

possibleTrees.add(new int[]{rr, cc, currTime + 1});

}//end if
}//end for

return possibleTrees;
}//end getTreeNeighbors


//check if in bounds
//check base case (epmty space)
//loop over directions and as long as trees contiue to burn increase time

//{current row, current column, current time}, put time in and take time back out

}
91 changes: 90 additions & 1 deletion src/FireTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

Expand All @@ -20,4 +21,92 @@ public void testTimeToBurnExample() {

assertEquals(expected, actual);
}
}

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

int matchR = 1;
int matchC = 1;

int expected = 0; // burns immediately
int actual = Fire.timeToBurn(forest, matchR, matchC);

assertEquals(expected, actual);
}//end testSingleTree

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

int matchR = 0;
int matchC = 0;

int expected = 0; //trees are too far to be burned
int actual = Fire.timeToBurn(forest, matchR, matchC);

assertEquals(expected, actual);
}//end testFarAwayTrees

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

int matchR = 1;
int matchC = 1;

int expected = 2;
int actual = Fire.timeToBurn(forest, matchR, matchC);

assertEquals(expected, actual);
}//end testTreesConnected

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

//out of bounds
int matchR = -1;
int matchC = 5;

assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
Fire.timeToBurn(forest, matchR, matchC);
});
}//end testOutOfBounds

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

int matchR = 1;
int matchC = 1;

int expected = 0;
int actual = Fire.timeToBurn(forest, matchR, matchC);

assertEquals(expected, actual);
}//end testNoFire



}//end file