holes


{draw lines representing cuts to the field;
drawing is done either with True of False (erasing);
only lines leading through an empty area are drawn - this makes
no difference when drawing but is important when erasing}
Procedure DrawCuts (Color: Boolean);
Var X, Y: Integer;
    I: Integer;
Begin
    For I := 1 to NumCutX do
        For X := 2*CutX[I].X1 to 2*CutX[I].X2 do
            If not Field[X, 2*CutX[I].Y-1]
                    and not Field[X, 2*CutX[I].Y+1] then
                Field[X, 2*CutX[I].Y] := Color;
    For I := 1 to NumCutY do
        For Y := 2*CutY[I].Y1 to 2*CutY[I].Y2 do
            If not Field[2*CutY[I].X+1, Y]
                    and not Field[2*CutY[I].X-1, Y] then
                Field[2*CutY[I].X, Y] := Color;
End; { DrawCuts }


{seed-fill the given area}
Procedure SeedFill (X, Y: Integer);
Begin
    QB := 1; QE := 1;
    Enqueue (X, Y);
    While (QB < QE) do
    Begin
        X := QX[QB]; Y := QY[QB];
        Inc (QB);
        If (X > 1) then Enqueue (X-1, Y);
        If (X <= MaxFldX) then Enqueue (X+1, Y);
        If (Y > 1) then Enqueue (X, Y-1);
        If (Y <= MaxFldY) then Enqueue (X, Y+1);
    End;
End; { SeedFill }


{count all the holes in the given situation}
Function CountHoles : Integer;
Var X, Y: Integer;
    Cnt: Integer;
Begin
    Cnt := 0;
    For X := 1 to MaxFldX do
        For Y := 1 to MaxFldY do
            If not Field[X,Y] then
            Begin
                SeedFill (X, Y);
                Inc (Cnt);
            End;
    CountHoles := Cnt;
End; { CountHoles }


{solve one situation}
Function SolveOne (N: Integer): Integer;
Begin
    ReadCuts (N);
    ConvertCuts;
    PrepareField;
    DrawCuts (true);
    SeedFill (1, 1);
    DrawCuts (false);
    SolveOne := CountHoles;
End;