Knowee
Questions
Features
Study Tools

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 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.Public Test Cases:# INPUT EXPECTED OUTPUT1 3 31 3W T TT W WW T T52 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 W16

Question

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 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.Public Test Cases:# INPUT EXPECTED OUTPUT1 3 31 3W T TT W WW T T52 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 W16

...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:

  1. Parse the input: Read the dimensions 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: Add the first tree that catches fire to the queue. This queue will be used to perform the BFS.

  3. Perform the BFS: While the queue is not empty, remove a tree from the queue, and for each of its adjacent trees, if the tree is not on fire and is not water, set it on fire and add it to the queue. Also, keep track of the maximum distance from the first tree that catches fire to any other tree, which will be the time it takes for the entire forest to catch fire.

  4. Output the result: The time it takes for the entire forest to catch fire is the maximum distance from the first tree that catches fire to any other tree.

Here is a Python code snippet that implements these steps:

from collections import deque

def bfs(forest, start):
    m, n = len(forest), len(forest[0])
    queue = deque([start])
    dist = {start: 0}
    while queue:
        x, y = queue.popleft()
        for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
            nx, ny = x + dx, y + dy
            if 0 <= nx < m and 0 <= ny < n and forest[nx][ny] == 'T' and (nx, ny) not in dist:
                dist[nx, ny] = dist[x, y] + 1
                queue.append((nx, ny))
    return max(dist.values())

m, n = map(int, input().split())
x, y = map(int, input().split())
forest = [list(input()) for _ in range(m)]
print(bfs(forest, (x-1, y-1)))

This code reads the input, performs the BFS, and prints the result. The bfs function performs the BFS and returns the time it takes for the entire forest to catch fire. The main part of the code reads the input, calls the bfs function, and prints the result.

This problem has been solved

Similar Questions

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.

Arero, who is 20 is one of the brave and positive young men who believe in planting trees for his future sustain. Guarding Yedero village’s new plantation (in shakiso, South Oromia Zone) aimed at conserving water and soil, Arero remembers the hard times his village faced due to the absence of trees in the surrounding area to protect it from floods , and seeing part of his own house washed away. The water would gather in the hills and, having no trees and buds to stop its force, would cause havoc in the village down below. An appeal made to the local administration and discussions that followed with CPAR Ethiopia project Officers brought the community together to plant 20C hectares on the hills around the village and terrace its hillsides. Arero looks happy now:“Today we can sleep feeling safe,” he explains. “There are no more floods threatening us. The soil is becoming richer for us to cultivate, and the thick grass, which grows under the trees, can be sold to provide us with an income.” A sense of respect for nature is developing in the Yedero village: if any of the trees are cut down, for firewood or to be sold, other trees are immediately planted instead of it. Arero and Village members now feel that in the future they can plant their own trees and fend off problems of scil and water loss. Arero is not alone in his concern; there are hundreds of people like him, young and old, who have realized the importance of planting trees and have pledged to carry on the work, year in, year out. Some ninety Addis-based individuals, for instance, have joined an environmental campaign called plant a Tree in Ethiopia’, with the aim of reversing the problem of desertification in the country. All such willing individuals sponsor the future planting of trees in Central Ethiopia and help the neediest farmers in the area to do so in their own compounds. Then would you answer the question, who you are and what you are for your communities? Would you mention any valuable thing you did or you are doing for the next generation will raise your name’? 1. Arero is a brave and positive man because A. He was born in the northern part of Ethiopia C. he is a young man B. He believes planting trees for future D. he helped the community 2. One of the major objectives of the yedero Gebriel Village's new plantation is ..... A. to protect the surrounding area from flood C. The water gather in the hills B. Remembering the hard time of his village D. to wash away parts of his own house 3. One of the following is not the result of trees in the village. A. The thick grass sold to provide us with an income C. Arero looks happy now B. The soil becomes fertile for cultivation D. Planting on the hills and terrace its hillsides.

Arero, who is 20 is one of the brave and positive young men who believe in planting trees for his future sustain. Guarding Yedero village’s new plantation (in shakiso, South Oromia Zone) aimed at conserving water and soil, Arero remembers the hard times his village faced due to the absence of trees in the surrounding area to protect it from floods , and seeing part of his own house washed away. The water would gather in the hills and, having no trees and buds to stop its force, would cause havoc in the village down below. An appeal made to the local administration and discussions that followed with CPAR Ethiopia project Officers brought the community together to plant 20C hectares on the hills around the village and terrace its hillsides. Arero looks happy now:“Today we can sleep feeling safe,” he explains. “There are no more floods threatening us. The soil is becoming richer for us to cultivate, and the thick grass, which grows under the trees, can be sold to provide us with an income.” A sense of respect for nature is developing in the Yedero village: if any of the trees are cut down, for firewood or to be sold, other trees are immediately planted instead of it. Arero and Village members now feel that in the future they can plant their own trees and fend off problems of scil and water loss. Arero is not alone in his concern; there are hundreds of people like him, young and old, who have realized the importance of planting trees and have pledged to carry on the work, year in, year out. Some ninety Addis-based individuals, for instance, have joined an environmental campaign called plant a Tree in Ethiopia’, with the aim of reversing the problem of desertification in the country. All such willing individuals sponsor the future planting of trees in Central Ethiopia and help the neediest farmers in the area to do so in their own compounds. Then would you answer the question, who you are and what you are for your communities? Would you mention any valuable thing you did or you are doing for the next generation will raise your name’? 1. Arero is a brave and positive man because A. He was born in the northern part of Ethiopia C. he is a young man B. He believes planting trees for future D. he helped the community 2. One of the major objectives of the yedero Gebriel Village's new plantation is ..... A. to protect the surrounding area from flood C. The water gather in the hills B. Remembering the hard time of his village D. to wash away parts of his own house 3. One of the following is not the result of trees in the village. A. The thick grass sold to provide us with an income C. Arero looks happy now B. The soil becomes fertile for cultivation D. Planting on the hills and terrace its hillsides. 5. What indicates as a sense of respect for nature is developing in the Yedero Gebrel village? A. An appeal made to the local administration to be discussed B. If any of the trees is cut down, other trees are immediately replaced. C. Village members feel that

Arero, who is 20 is one of the brave and positive young men who believe in planting trees for his future sustain. Guarding Yedero village's new plantation (in shakiso, South Oromia Zone) aimed at conserving water and soil, Arero remembers the hard times his village faced due to the absence of trees in the surrounding area to protect it from floods and seeing part of his own house washed away. The water would gather in the hills and, having no trees and buds to stop its force, would cause havoc in the village down below. An appeal made to the local administration and discussions that followed with CPAR-Ethiopia project Officers brought the community together to plant 200 hectares on the hills around the village and terrace its hillsides. Arero looks happy now:"Today we can sleep feeling safe," he explains. "There are no more floods threatening us. The soil is becoming richer for us to cultivate, and the thick grass, which grows under the trees, can be sold to provide us with an income." A sense of respect for nature is developing in the Yedero village: if any of the trees are cut down, for firewood or to be sold, other trees are immediately planted instead of it. Arero and Village members now feel that in the future they can plant their own trees and fend off problems of soil and water loss. Arero is not alone in his concern; there are hundreds of people like him, young and old, who have realized the importance of planting trees and have 7 new plantation is A. to protect the surrounding area from flood C. The water gather in the hills B. Remembering the hard time of his village D. to wash away parts of his own house 3. One of the following is not the result of trees in the village. C. Arero looks happy now A. The thick grass sold to provide us with an income B. The soil becomes fertile for cultivation D. Planting on the hills and terrace its hillsides

Crown fire mostly takes place in which of the following forests ?(1) Dense Forest(2) Thorn Forest(3) Savanna Forest(4) Alpine Fores

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.