Polar Bear

Time limit5sMemory limit128 MB

Summary
Simulate Conway's Game of Life on concentric rings with special opposite-cell neighbors, then report the live count and lexicographic first and last live cells after g steps.
Level

Medium6 of 10

Topics
Simulation, Array, Implementation
Solved
No attempts yet

Problem

This is Conway's Game of Life played on polar-coordinate graph paper instead of a rectangular grid. The board has mm concentric rings and nn radial lines. The rings are numbered from the outside inward, 00 for the outermost ring up to m−1m-1 for the innermost ring. Because each ring is divided by the nn radial lines, it contains exactly nn cells. A cell is identified by a pair (r,c)(r, c): its ring rr (0≤r≤m−10 \le r \le m-1) and its position cc (0≤c≤n−10 \le c \le n-1), counted clockwise from a fixed radius. The number of radial lines nn is always even.

At every tick all cells update simultaneously under the usual Game of Life rules: a dead cell with exactly three live neighbors becomes live in the next generation; a live cell with fewer than two or more than three live neighbors becomes dead; every other cell keeps its state.

Every cell has exactly eight neighbors, defined as follows (all position indices are taken modulo nn):

  • An interior cell (r,c)(r, c) with 0<r<m−10 < r < m-1 has the eight neighbors (r,c±1)(r, c\pm 1), (r−1,c−1)(r-1, c-1), (r−1,c)(r-1, c), (r−1,c+1)(r-1, c+1), (r+1,c−1)(r+1, c-1), (r+1,c)(r+1, c), and (r+1,c+1)(r+1, c+1).
  • A cell (0,c)(0, c) on the outer ring has the five ordinary neighbors (0,c±1)(0, c\pm 1), (1,c−1)(1, c-1), (1,c)(1, c), (1,c+1)(1, c+1), together with the diametrically opposite cell (0,c+n/2)(0, c + n/2) on the same ring and that cell's two ring neighbors (0,c+n/2−1)(0, c + n/2 - 1) and (0,c+n/2+1)(0, c + n/2 + 1).
  • A cell (m−1,c)(m-1, c) on the inner ring has the five ordinary neighbors (m−1,c±1)(m-1, c\pm 1), (m−2,c−1)(m-2, c-1), (m−2,c)(m-2, c), (m−2,c+1)(m-2, c+1), together with the diametrically opposite cell (m−1,c+n/2)(m-1, c + n/2) and its two ring neighbors (m−1,c+n/2−1)(m-1, c + n/2 - 1) and (m−1,c+n/2+1)(m-1, c + n/2 + 1).

Input

The input contains several test cases. Each test case begins with two positive integers mm and nn (3≤m≤1003 \le m \le 100, 6≤n≤1006 \le n \le 100, with nn even), the number of rings and the number of radial lines. Next comes a positive integer kk followed by kk distinct pairs of integers (which may span several lines); each pair r cr\ c gives the ring rr and position cc of one initially live cell. After the pairs comes a single nonnegative integer gg (g≤500g \le 500), the number of generations to simulate. The last test case is followed by a line containing two zeros, which is not processed.

Output

For each test case, print Case X: (where X is the test case number, starting from 11) followed by five integers: the number of live cells after gg generations, then r1 c1r_1\ c_1, the position of the lexicographically first live cell, and r2 c2r_2\ c_2, the position of the lexicographically last live cell. Cells are ordered lexicographically by ring number and then by position. If no cells are alive, print 0 -1 -1 -1 -1 for those five integers.

Examples1

  1. Example 1

    Input
    6 8
    3
    0 7 0 0
    0 1
    1
    4 6
    1
    1 0
    10
    0 0
    
    Expected output
    Case 1: 3 0 0 1 0
    Case 2: 0 -1 -1 -1 -1