Bacteria (Large)
Time limit5sMemory limit512 MB
Given initially filled rectangles on a grid evolving by a north-west neighbor rule, compute the seconds until no bacterium remains.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Simulation, Matrix
- Solved
- No attempts yet
Problem
Bacteria live on an infinite grid of cells, and each bacterium occupies one cell.
Every second all of the following changes happen at the same time.
- A bacterium with no neighbor to its north and no neighbor to its west dies.
- An empty cell whose north neighbor and west neighbor both hold a bacterium gains a new bacterium.
When you look at the grid, the number of bacteria is finite and at least one, and the occupied cells form one or more rectangular regions. Compute how many seconds pass before every bacterium is dead.
The grid below starts with 6 bacteria and is empty after 6 seconds. A 1 is a cell with a bacterium and a 0 is an empty cell. The pictures are one second apart.
000010
011100
010000
010000
000000
000000
001110
011000
010000
000000
000000
000110
001100
011000
000000
000000
000010
000110
001100
000000
000000
000000
000010
000110
000000
000000
000000
000000
000010
000000
000000
000000
000000
000000
000000
Input
The first line has , the number of test cases. Each test case is given in this form.
- One line with , the number of rectangular regions that initially contain bacteria.
- lines with four space separated integers . Every cell whose X coordinate is between and inclusive and whose Y coordinate is between and inclusive contains a bacterium.
The rectangles may overlap.
North is the direction of decreasing Y coordinate. West is the direction of decreasing X coordinate.
Limits
Output
For each test case print one line of the form Case #N: T, where is the test case number counted from 1 and is the number of seconds until every bacterium is dead.