program Gnome; const maxC=300; maxR=150; nic=' '; var i,j,R, C: integer; a: array[1..maxC, 1..maxR] of char; b: array[1..maxC] of Boolean; count,pred,next: array[1..maxC] of integer; body: integer; pocet_barveni: integer; procedure info; forward; procedure barvi(x, y: integer; Ch: Char); label Ex; var i: Integer; begin if (x <= 0) or (y <= 0) or (x > C) or (y > R) or (Ch <> a[x, y]) then GoTo Ex; if a[x,y]=nic then goto Ex; b[x] := true; a[x,y]:=nic; inc(pocet_barveni); {i := x - 1; while (i > 0) and (count[i] = 0) do Dec(i);} i:=pred[x]; barvi(i, y,ch); i:=next[x]; {i:=x+1; while (i <= C) and (count[i] = 0) do Inc(I);} barvi(i, y,ch); barvi(x,y+1,ch); barvi(x,y-1,ch); Ex: end; procedure move(x,y: integer); label Ex; var i, j, k: integer; begin if (x <= 0) or (y <= 0) then GoTo Ex; i := x; x:=0; while (i>0) and (X < C) do begin inc(x); if count[x] > 0 then Dec(I); end; if I <> 0 then GoTo Ex; if Count[x] < y then GoTo Ex; pocet_barveni:=0; barvi(x,y,a[x,y]); inc(body, sqr(pocet_barveni-2)); for i:=1 to C do if (count[i] <> 0) and B[i] then begin k:=0; for j:=1 to count[i] do begin if a[i, j] <> nic then begin Inc(k); a[i, k] := a[i, j]; end; end; for j:=K+1 to count[i] do a[i, j]:=nic; count[i]:=k; end; Ex: end; procedure info; var i,j: integer; begin for i:=1 to R do begin for j:=1 to C do write(a[j,i]); writeln; end; writeln; end; var Z, InX, InY, InN: Integer; begin Readln(z); while Z > 0 do begin Dec(Z); Readln(C, R); for i:=1 to C do Count[i] := R; for J := 1 to R do begin for I := 1 to C do Read(A[I, J]); Readln; end; body := 0; Readln(InN); while InN > 0 do begin Dec(InN); Readln(InX, InY); { info; } i:=0; for j:=1 to C do begin pred[j] := i; if count[j] <> 0 then i:=j; end; i:=C + 1; for j:=C downto 1 do begin next[j] := i; if count[j] <> 0 then i:=j; end; for i:=1 to C do b[i] := false; Move(InX, InY); { writeln('body = ',body);} { info;} { writeln('---');} end; j:=0; for i:= 1 to c do if count[i] <> 0 then j:=1; if j = 0 then Inc(Body,1000); writeln('Game over!'); writeln('Score dosazene v teto hre je ', body, ' bodu.'); writeln('Byli bychom radi, kdybyste si zahrali jeste jednou.'); writeln('Prejete si hrat znovu?'); writeln('Prijemnou zabavu Vam preje firma ACMTENDO.'); end; end.