Rotating Disks
Time limit1sMemory limit512 MB
Simulate T rounds of rotating selected concentric disks, erasing adjacent equal numbers, or adjusting all numbers toward the average, then report the final sum.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Array, Math
- Solved
- No attempts yet
Problem
Disks with radii 1, 2, ..., N rest on the floor in decreasing order of size, and all of them share the same center. If a disk has radius i, it is called the i-th disk. Each disk has M integers written on it, and the position of the j-th number on the i-th disk is denoted (i, j). The positions satisfy the following.
- (i, 1) is adjacent to (i, 2) and (i, M).
- (i, M) is adjacent to (i, M-1) and (i, 1).
- (i, j) is adjacent to (i, j-1) and (i, j+1). (2 ≤ j ≤ M-1)
- (1, j) is adjacent to (2, j).
- (N, j) is adjacent to (N-1, j).
- (i, j) is adjacent to (i-1, j) and (i+1, j). (2 ≤ i ≤ N-1)
The figure below shows the case N = 3, M = 4.

The disks rotate independently. When disk 2 rotates, the other disks do not rotate. A disk rotates with respect to the positions of the numbers, and after the rotation the positions of the numbers must match those before the rotation.
The figures below show examples of rotating disks.
We want to rotate the disks T times in total as follows. The rotations are fixed in advance, and the variables used for the i-th rotation are xi, di, ki.
-
Rotate every disk whose number is a multiple of xi by ki positions in direction di. If di is 0 the direction is clockwise, and if di is 1 the direction is counterclockwise.
-
If any numbers remain on the disks, find all adjacent pairs of numbers that are equal.
- If such pairs exist, erase all adjacent equal numbers from the disks.
- If no such pairs exist, compute the average of the numbers on the disks, subtract 1 from every number greater than the average, and add 1 to every number smaller than the average.
After rotating the disks T times, find the sum of the numbers on the disks.
Input
The first line gives N, M, and T.
Starting from the second line, N lines give the numbers on the disks. The j-th number on the i-th line is the number written at (i, j).
The following T lines give xi, di, and ki.
Output
After rotating the disks T times, print the sum of the numbers on the disks.
Constraints
- 2 ≤ N, M ≤ 50
- 1 ≤ T ≤ 50
- 1 ≤ numbers on the disks ≤ 1,000
- 2 ≤ xi ≤ N
- 0 ≤ di ≤ 1
- 1 ≤ ki < M


