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
91 changes: 84 additions & 7 deletions src/Fire.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
public class Fire {
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;


public class Fire
{
/**
* Returns how long it takes for all vulnerable trees to be set on fire if a
* match is lit at a given location.
Expand Down Expand Up @@ -29,15 +36,85 @@ public class Fire {
* At time 2, the trees adjacent to those catch as well. At time 8, the last tree to catch is at
* (0,6). In this example, there is one tree that never burns, so it is not included in the time calculation.
*
*
* // HINT: when adding to your BFS queue, you can include more information than
* // just a location. What other information might be useful?
* @param forest a 2d array where t represents a tree and . represents the ground
* @param matchR The row the match is lit at
* @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) {
// HINT: when adding to your BFS queue, you can include more information than
// just a location. What other information might be useful?
return -1;
public static int timeToBurn(char[][] forest, int matchR, int matchC)
{
int rows = forest.length; //number of rows in the grid
int cols = forest[0].length; //cols in the grid

//if the start isnt a tree it wont burn //base case
if (forest[matchR][matchC] != 't') return 0;


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

int maxTime = 0;
int[] match = { matchR, matchC, 0 };
queue.add(match);


//int maxTime = 0;

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

while (!queue.isEmpty())
{
int[] current = queue.poll();
int curR = current[0];
int curC = current[1];
//int time = current[2];

if(visited[curR][curC])
{
continue;
}

visited[curR][curC] = true;

queue.addAll(neighborTrees(forest, current));

maxTime = current[2];

}

return maxTime;
}
}
public static List<int[]> neighborTrees(char[][] forest, int[] location)
{
int curR = location[0];
int curC = location[1];
int depth = location[2] + 1;
List<int[]> neighbors = new ArrayList<>();

int[][] directions =
{
{-1, 0},
{1, 0},
{0, -1},
{0, 1}
};

for (int[] move : directions)
{
int newR =curR + move[0];
int newC =curC + move[1];


if(newR >= 0 && newR < forest.length && newC >= 0 && newC < forest[newR].length && forest[newR][newC] == 't')
{
int[] validMove = { newR, newC, depth };
neighbors.add(validMove);
}
}
return neighbors;
}
}

55 changes: 55 additions & 0 deletions src/FireTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import org.junit.jupiter.api.Test;

public class FireTest {
Expand All @@ -20,4 +22,57 @@ public void testTimeToBurnExample() {

assertEquals(expected, actual);
}

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

/*
* [
* {1, 4, 5}
* {}
* {}
* ]
*/


int[] location = {2, 4, 4 };
List<int[]> result = Fire.neighborTrees(forest, location);

assertEquals(4, result.size());
assertEquals(1, result.get(0)[0]);
assertEquals(4, result.get(0)[1]);
assertEquals(5, result.get(0)[2]);
assertEquals(3, result.get(1)[0]);
assertEquals(4, result.get(1)[1]);
assertEquals(5, result.get(1)[2]);
assertEquals(2, result.get(2)[0]);
assertEquals(3, result.get(2)[1]);
assertEquals(5, result.get(2)[2]);
assertEquals(2, result.get(3)[0]);
assertEquals(5, result.get(3)[1]);
assertEquals(5, result.get(3)[2]);
}

@Test
public void test_middleOfNoWhere()
{
char[][] forest =
{
{ 't', '.', '.', 't', 't', 't', 't', '.', 't' },
{ '.', '.', 't', '.', '.', '.', '.', '.', 't' },
{ '.', '.', 't', '.', 't', '.', 't', '.', 't' },
{ 't', 't', 't', 't', '.', '.', '.', '.', 't' }
};
int[] location = {2, 6, 4 };
List<int[]> result = Fire.neighborTrees(forest, location);
assertEquals(0, result.size());
}
}