Wizard Shark and Firestorm
InterviewTime limit1sMemory limit512 MB
Repeat Q times: rotate every 2^L x 2^L block 90 degrees clockwise, then subtract 1 from each ice cell that has fewer than 3 ice neighbors. Output the total ice and the largest connected ice chunk.
- Level
Medium6 of 10
- Topics
- Implementation, Simulation, Matrix, BFS
- Solved
- No attempts yet
Problem
Wizard Shark can cast Firestorm by combining Fireball and Tornado. Today he wants to practice Firestorm on an ice sheet divided into a grid of size . Position means row and column of the grid, and means the amount of ice at . If is 0, there is no ice there.
To cast Firestorm, he must decide on a level for each cast. Firestorm first divides the grid into subgrids of size . Then it rotates every subgrid 90 degrees clockwise. After that, any cell that is not adjacent to at least 3 cells containing ice loses 1 from its amount of ice. The cells adjacent to are , , , and . The integers written in the cells of the figures below are just labels to distinguish the cells.
Wizard Shark wants to cast Firestorm times in total. After casting all the Firestorms, find the following two values.
- The sum of the remaining ice
- The number of cells occupied by the largest chunk of remaining ice
If a cell containing ice is adjacent to another cell containing ice, the two cells are said to be connected. A chunk is a set of connected cells.
Input
The first line gives and . From the second line, lines give the amount of ice in each cell of the grid. The integer given at position in the -th line is .
The last line gives the levels that Wizard Shark cast, in order.
Output
Print the sum of the remaining ice on the first line, and the number of cells occupied by the largest chunk on the second line. If there is no chunk, print 0.


