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: 45 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,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;
boolean[][] visited = new boolean[forest.length][forest[0].length];
return timeToBurn(forest, matchR, matchC, 0, visited);
}


public static int timeToBurn(char[][] forest, int r, int c, int time, boolean[][] visited){

Queue<int[]> trees = new LinkedList<>();
trees.add(new int[] {r, c, time});
visited[r][c] = true;
int maxTime = 0;

while(!trees.isEmpty()){
int[] current = trees.poll();
System.out.println(current[2]);
int currentTime = current[2];

maxTime = currentTime;
for(int[] dir: directions){

int[] point = {current[0]+dir[0], current[1] +dir[1], currentTime +1};

if(point[0] >= 0 && point[0]<forest.length &&
point[1] >=0 && point[1] <forest[0].length &&
!visited[point[0]][point[1]] &&
forest[point[0]][point[1]] == 't'){

visited[point[0]][point[1]] = true;
trees.add(point);
}

}
}

return maxTime ;
}

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


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

assertEquals(expected, actual);
}

}