Under construction

Still being built. Things may change

Gnome

Time limit1sMemory limit128 MB

Summary
Simulate Same-Gnome moves on a letter board, clearing connected groups with gravity and scoring each removal.
Level

Medium4 of 10

Topics
Simulation, Implementation, BFS, Matrix
Solved
No attempts yet

Problem

One of the least pleasant parts of a police officer's job during civil unrest is waiting. Picture a few hours in the rain, waiting to see which way an angry crowd will move. In some ways it is worse than a dentist's waiting room, where at least it does not rain. So the police command decided to issue every officer a waterproof handheld console that runs Same-Gnome, one variant of the legendary game Gnome.

Your job is to write a simulator for that game. It has to be simple enough to run on the portable PMD-85 (the so called PMD-Pilot). The rules are short. The board is N x M cells, and every cell holds exactly one colored stone. The goal is to remove as many stones as possible and to score as many points as possible while doing it. Stones come off the board by these rules.

  • A stone can be removed only if it touches at least one stone of the same color. Touching means up, down, left and right, not diagonally.
  • Removing one stone also removes the whole connected region of same colored stones it belongs to.
  • The board sits in a gravity field, so once stones disappear the stones above them fall and fill the gap.
  • If empty columns appear after the stones disappear and the ones above fall, the columns to the right shift left so that no empty column is left.
  • Removing K stones in one move scores (K−2)2(K-2)^2 points.
  • Clearing every stone off the board this way during the game scores an extra 1000 points.

Input

The first line has a positive integer Z, followed by Z game descriptions. The first line of each description has the number of columns C and the number of rows R of the board, separated by a space (1≤R<1501 \le R < 150, 1≤C<3001 \le C < 300). The next R lines give the rows of the board from top to bottom. Each of those lines has exactly C uppercase letters from A to Z, the colors of that row's stones from left to right.

The board is followed by the list of moves the player made, that is the coordinates of the stones to remove. First comes a line with the number of moves M. Then come M lines, each with two integers I and J separated by a space. I (1≤I≤C1 \le I \le C) is the column number of the chosen stone counted from the left starting at one, and J (1≤J≤R1 \le J \le R) is the row number counted from the bottom, also starting at one.

Output

Simulate the whole game and remove connected regions of same colored stones in the order given on the input. If there is no stone at the given position, or the chosen stone cannot be removed, ignore that move and go on to the next one. For each game description print these five lines.

Game over!
Score dosazene v teto hre je S bodu.
Byli bychom radi, kdybyste si zahrali jeste jednou.
Prejete si hrat znovu?
Prijemnou zabavu Vam preje firma ACMTENDO.

S is the score earned under the rules above. Each sentence goes on its own line.

Examples1

  1. Example 1

    Input
    2
    3 2
    GZZ
    ZZG
    2
    2 1
    2 1
    6 3
    ABABAB
    BABABA
    ABABAA
    11
    6 1
    5 1
    4 1
    3 1
    2 1
    1 1
    2 1
    3 1
    4 1
    5 1
    6 1
    
    Expected output
    Game over!
    Score dosazene v teto hre je 1004 bodu.
    Byli bychom radi, kdybyste si zahrali jeste jednou.
    Prejete si hrat znovu?
    Prijemnou zabavu Vam preje firma ACMTENDO.
    Game over!
    Score dosazene v teto hre je 5 bodu.
    Byli bychom radi, kdybyste si zahrali jeste jednou.
    Prejete si hrat znovu?
    Prijemnou zabavu Vam preje firma ACMTENDO.