Knowee
Questions
Features
Study Tools

QUESTION 04 bookmark_border Roco is an island near Africa which is very prone to forest fire. Forest fire is such that it destroys the complete forest. Not a single tree is left. This island has been cursed by God , and the curse is that whenever a tree catches fire, it passes the fire to all its adjacent tree in all 8 directions, North, South, East, West, North-East, North-West, South-East, and South-West. And it is given that the fire is spreading every minute. Trees (indicated by T) and water (indicated by W) spots are arranged in a grid of MxN M-rows N-Columns. Your task is that given the location of the first tree that catches fire, determine how long would it take for the entire forest to be on fire. You may assume that the lay out of the forest is such that the whole forest will catch fire for sure and that there will be at least one tree in the forest. Input Format: First line contains two integers, M, N, space separated, giving the size of the forest in terms of the number of rows and columns respectively The next line contains two integers X,Y, space separated, giving the coordinates of the first tree that catches the fire The next M lines, where ith line containing N characters each of which is either T or W, giving the position of the Tree and Water in the ith row of the forest. Output Format: Single integer indicating the number of minutes taken for the entire forest to catch fire Constraints: 3 ≤ M ≤ 20 3 ≤ N ≤ 20 Sample Input - 1: 3 3 1 3 W T T T W W W T T Sample Output - 1: 5 Explanation: In the second minute, tree at (1,2) catches fire. In the third minute, the tree at (2,1) catches fire, fourth minute tree at (3,2) catches fire and in the fifth minute the last tree at (3,3) catches fire. Sample Input - 2: 6 6 1 6 W T T T T T T W W W W W W T T T T T W W W W W T T T T T T T T W W W W W Sample Output - 2: 16

Question

QUESTION 04 bookmark_border Roco is an island near Africa which is very prone to forest fire. Forest fire is such that it destroys the complete forest. Not a single tree is left. This island has been cursed by God , and the curse is that whenever a tree catches fire, it passes the fire to all its adjacent tree in all 8 directions, North, South, East, West, North-East, North-West, South-East, and South-West. And it is given that the fire is spreading every minute. Trees (indicated by T) and water (indicated by W) spots are arranged in a grid of MxN M-rows N-Columns.

Your task is that given the location of the first tree that catches fire, determine how long would it take for the entire forest to be on fire. You may assume that the lay out of the forest is such that the whole forest will catch fire for sure and that there will be at least one tree in the forest.

Input Format:

First line contains two integers, M, N, space separated, giving the size of the forest in terms of the number of rows and columns respectively

The next line contains two integers X,Y, space separated, giving the coordinates of the first tree that catches the fire

The next M lines, where ith line containing N characters each of which is either T or W, giving the position of the Tree and Water in the ith row of the forest.

Output Format:

Single integer indicating the number of minutes taken for the entire forest to catch fire

Constraints:

3 ≤ M ≤ 20

3 ≤ N ≤ 20

Sample Input - 1:

3 3

1 3

W T T

T W W

W T T

Sample Output - 1:

5

Explanation:

In the second minute, tree at (1,2) catches fire. In the third minute, the tree at (2,1) catches fire, fourth minute tree at (3,2) catches fire and in the fifth minute the last tree at (3,3) catches fire.

Sample Input - 2:

6 6

1 6

W T T T T T

T W W W W W

W T T T T T

W W W W W T

T T T T T T

T W W W W W

Sample Output - 2:

16

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem can be solved using the Breadth-First Search (BFS) algorithm. Here are the steps to solve this problem:

  1. Parse the input: Read the size of the forest (M, N), the coordinates of the first tree that catches fire (X, Y), and the layout of the forest.

  2. Initialize a queue and a 2D array to keep track of the time it takes for each tree to catch fire. The array should be the same size as the forest, and initially, all its values should be set to a large number (e.g., infinity).

  3. Add the first tree that catches fire to the queue and set its corresponding time in the array to 0.

  4. While the queue is not empty, remove a tree from the queue. For each of its adjacent trees in all 8 directions, if the tree is not on fire yet (i.e., its time in the array is still infinity), update its time in the array to be one more than the current tree's time, and add it to the queue.

  5. After all trees have been processed, the time it takes for the entire forest to catch fire is the maximum value in the array.

This algorithm works because BFS ensures that we process trees in the order they catch fire, and for each tree, we always use the earliest time it can catch fire. The time complexity of this algorithm is O(MN), where M and N are the number of rows and columns in the forest, respectively.

This problem has been solved

Similar Questions

QUESTION 04 bookmark_border Roco is an island near Africa which is very prone to forest fire. Forest fire is such that it destroys the complete forest. Not a single tree is left. This island has been cursed by God , and the curse is that whenever a tree catches fire, it passes the fire to all its adjacent tree in all 8 directions, North, South, East, West, North-East, North-West, South-East, and South-West. And it is given that the fire is spreading every minute. Trees (indicated by T) and water (indicated by W) spots are arranged in a grid of MxN M-rows N-Columns. Your task is that given the location of the first tree that catches fire, determine how long would it take for the entire forest to be on fire. You may assume that the lay out of the forest is such that the whole forest will catch fire for sure and that there will be at least one tree in the forest. Input Format: First line contains two integers, M, N, space separated, giving the size of the forest in terms of the number of rows and columns respectively The next line contains two integers X,Y, space separated, giving the coordinates of the first tree that catches the fire The next M lines, where ith line containing N characters each of which is either T or W, giving the position of the Tree and Water in the ith row of the forest. Output Format: Single integer indicating the number of minutes taken for the entire forest to catch fire Constraints: 3 ≤ M ≤ 20 3 ≤ N ≤ 20 Sample Input - 1: 3 3 1 3 W T T T W W W T T Sample Output - 1: 5 Explanation: In the second minute, tree at (1,2) catches fire. In the third minute, the tree at (2,1) catches fire, fourth minute tree at (3,2) catches fire and in the fifth minute the last tree at (3,3) catches fire. Sample Input - 2: 6 6 1 6 W T T T T T T W W W W W W T T T T T W W W W W T T T T T T T T W W W W W Sample Output - 2: 16

Forest FireRoco is an island near Africa which is very prone to forest fire. Forest fire is such that it destroys the complete forest. Not a single tree is left.This island has been cursed by God , and the curse is that whenever a tree catches fire, it passes the fire to all its adjacent tree in all 8 directions,North, South, East, West, North-East, North-West, South-East, and South-West.And it is given that the fire is spreading every minute in the given manner, i.e every tree is passing fire to its adjacent tree.Suppose that the forest layout is as follows where T denotes tree and W denotes water.Your task is that given the location of the first tree that catches fire, determine how long would it take for the entire forest to be on fire. You may assume that the lay out of the forest is such that the whole forest will catch fire for sure and that there will be at least one tree in the forestConstraints:Input Format:First line contains two integers, M, N, space separated, giving the size of the forest in terms of the number of rows and columns respectively.The next line contains two integers X,Y, space separated, giving the coordinates of the first tree that catches the fire.The next M lines, where ith line containing N characters each of which is either T or W, giving the position of the Tree and Water in the  ith row of the forest.Output Format:Single integer indicating the number of minutes taken for the entire forest to catch fireConstraints:3 ≤ M ≤ 203 ≤ N ≤ 20Example:Example 1Input 1:6 61 6W T T T T TT W W W W WW T T T T TW W W W W TT T T T T TT W W W W WOutput:16Example 2:Input:3 31 3W T TT W WW T TOutput:5Explanation:Explanation - Example 2:In the second minute, tree at (1,2) catches fire,in the third minute,the tree at (2,1) catches fire,the fourth minute tree at (3,2) catches fire and in the fifth minute the last tree at (3,3) catches fire.

In West Africa, the rainforests cover a wide strip ___Q1___ the coast from Sierra Leone to Gabon. In the last century these forests ___Q2___ mostly uninhabited. The Europeans arrived and soon began chopping ___Q3___ the trees for timber and to make way for massive plantations of cocoa, peanuts and cotton. Today, two-third of the West African forests ___Q4___ gone. QUESTION 03 bookmark_border Select the correct answer Q4 radio_button_unchecked has radio_button_unchecked is radio_button_unchecked have radio_button_checked were

Question 8SavedDetermine whether the following is true or false.Most of the fires were likely to have occurred on land that was previously damaged or cleared. However, these fires could still threaten untouched rainforest as they could spread to unintended areas.TrueFalseI'm not sure

In West Africa, the rainforests cover a wide strip ___Q1___ the coast from Sierra Leone to Gabon. In the last century these forests ___Q2___ mostly uninhabited. The Europeans arrived and soon began chopping ___Q3___ the trees for timber and to make way for massive plantations of cocoa, peanuts and cotton. Today, two-third of the West African forests ___Q4___ gone.QUESTION 14bookmark_borderSelect the correct answerQ1radio_button_uncheckedforradio_button_uncheckedonradio_button_checkedalongradio_button_uncheckedof

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.