Problem E
Forest Fires
Fires can spread in a forest more easily if the trees in the
forest are densely packed – it makes it easy for the fire to
jump from tree to tree. Write a forest fire simulator which
‘grows’ a forest tree-by-tree. As the forest grows, your
simulator should answer queries about whether a fire starting
at tree
The forest is modeled as a grid of size
Fire spreads directly from tree
The simulator relies on a random number generator, which is defined as:
where each simulation starts with a different ‘seed’ for
Given a seed
-
Initialize: let
. -
Stopping condition: if
, then stop. -
Place a tree: choose a random square
from the random number generator. If the square numbered is occupied by a tree, generate another value of in the same way. Keep doing this until is for an unoccupied square. Place a tree at square , and call this tree . -
Choose
: compute , and let . -
Choose
: compute , and let . It is okay if and represent the same tree. -
Fire query: discover whether a fire started at tree
can burn to tree . -
Increment: let
. -
Loop: go back to step 2.
Report a summary of the fire queries every
Input
Input contains up to
Output
For each test case, after each set of
Sample Input 1 | Sample Output 1 |
---|---|
4 1000 22 2000 25 1000 |
9 1 0 0 0 0 0 0 0 0 7 1 0 0 1 0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 1 2 0 0 0 0 1 1 0 0 |