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
49 changes: 48 additions & 1 deletion src/Fire.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import java.util.LinkedList;
import java.util.Queue;
import java.util.ArrayList;
import java.util.List;


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 +44,47 @@ 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;
boolean[][] onFire = new boolean[forest.length][forest[0].length];
Queue<int[]> queue = new LinkedList<>();
int maxDepth = 0;
queue.add(new int[]{matchR, matchC, maxDepth});

while (!queue.isEmpty()) {
int[] current = queue.poll();
int currR = current[0];
int currC = current[1];
int depth = current[2] + 1;

if(onFire[currR][currC]) {
continue;
}

onFire[currR][currC] = true;

List<int[]> moves = possibleMoves(forest, current);

for(int[] move : moves) {
queue.add(new int[]{move[0], move[1], depth});

}
maxDepth = Math.max(maxDepth, depth);
}
return maxDepth-1;
}
public static List<int[]> possibleMoves(char[][] forest, int[] curr) {
int[][] directions = {{-1,0},{1,0},{0,-1},{0,1}};
List<int[]> moves = new ArrayList<>();
for (int[] dir : directions) {
int newROW = curr[0] + dir[0];
int newCOL = curr[1] + dir[1];
if (newROW>=0&&
newROW<forest.length&&
newCOL>=0&&
newCOL<forest[newROW].length&&
forest[newROW][newCOL]!='.') {
moves.add(new int[]{newROW,newCOL});
}
}
return moves;
}
}
118 changes: 118 additions & 0 deletions src/FireTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,124 @@ public void testTimeToBurnExample() {
int expected = 8;
int actual = Fire.timeToBurn(forest, matchR, matchC);

assertEquals(expected, actual);
}
@Test
public void testTimeToBurnExample2() {
char[][] forest = {
{'t','.','.','t','t','t','t','.','t'},
{'.','.','t','.','.','.','.','.','t'},
{'.','.','t','t','t','t','t','t','t'},
{'t','t','t','t','.','.','.','.','.'}
};

int matchR = 0;
int matchC = 3;

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

assertEquals(expected, actual);
}
@Test
public void testTimeToBurnExample3() {
char[][] forest = {
{'t','.','t','t','t','t','t','.','t'},
{'t','.','t','.','.','.','t','.','t'},
};

int matchR = 0;
int matchC = 3;

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

assertEquals(expected, actual);
}
@Test
public void testTimeToBurnExample4() {
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 = 3;

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

assertEquals(expected, actual);
}
@Test
public void testTimeToBurnExample5() {
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 = 3;
int matchC = 4;

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

assertEquals(expected, actual);
}
@Test
public void testTimeToBurnExample6() {
char[][] forest = {
{'t','t','t','t','t','t','t','t','t'}
};

int matchR = 0;
int matchC = 1;

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

assertEquals(expected, actual);
}
@Test
public void testTimeToBurnExample7() {
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','t','t','t'},
{'t','t','t','t'},
{'t','t','t','t'},
{'t','t','t','t'},
{'t','t','t','t'},
};

int matchR = 4;
int matchC = 1;

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

assertEquals(expected, actual);
}
@Test
public void testTimeToBurnExample8() {
char[][] forest = {
{'t','t','t','.','.','.','t','t','t'}
};

int matchR = 0;
int matchC = 4;

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

assertEquals(expected, actual);
}
}