After this exercise was added I've been hinted that the game is known as
Same Game originating back in 1985
- it is almost "ancient"!
This is a well-known game: the box is filled with the cubes of different colors and player can remove groups of the same colors consisting of blocks with their sides touching each other. After each move the cubes above the empty area fall down because of gravity. When the whole column is cleared it collapses (blocks on the right are moved leftwards to close the gap). For example:
1 2 3 2 1 - - 2 1 - 2 -
1 3[4]2 1 2 - 2 1 - 2 -
2 4 4 1 2 3[3]1 2 2 1 -
1 2 3 2 1 2 3 2 1 2 2 -
From initial position player chooses to remove 4
at the position 2,2
(with 0,0
at leftmost bottom) and two
additional 4
-s below it are removed, then 2
, 3
and 3
from above fall down. At the next move player removes
3
at the position 2,1
and two neighboring 3
-s are removed also, so 2
falls down and the rightmost column shifts
to the left to close the gap.
The game continues until all cubes are removed.
Player gains 1
point for removing group of a single cube. Group of two cubes costs 3
points, while group of three
costs 6
and so on, with the formula
score = count * (count + 1) / 2
i.e. progression is one of triangular numbers.
Your goal is by the given initial position and sequence of moves to tell the final score of the game.
Try to play with the demo above to get better understanding of rules and scoring.
Input data will contain:
N
(i.e. you are to deal with the square box of N*N
dimension);N
lines of N
digits (color indices) each representing the game field (or the box);K
described below;K
pairs Xi
and Yi
(separated with comma-and-space) - zero-based coordinates of moves
(where Y=0
is the bottom line and X=0
is the leftmost column).Answer should contain a single value - the final score.
Example:
input data:
5
14111
23334
43212
33323
21232
12
0 4, 3 0, 0 1, 2 2, 1 1, 4 2, 3 2, 0 1, 3 2, 3 1, 3 0, 2 0
answer:
54
Let's verify example step-by-step. Firstly we remove 1
in the upper left corner. Next goes 3
from the
bottom row and so on:
1
for 1 point3
for 1 point3
s for 21 points1
for 1 point4
s for 3 points2
for 1 point3
for 1 point2
s for 3 points (column collapse)4
for 1 point3
for 1 point2
s for 10 points (collumn collapse)1
s for 10 points