Cod sursa(job #35172)

Utilizator Bluedrop_demonPandia Gheorghe Bluedrop_demon Data 21 martie 2007 21:15:15
Problema Jocul Flip Scor 40
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.85 kb
{
    Jocul Flip
}

Program Flip;

Type Matrice = array[1..16,1..16] of Longint;
     Vector = array[1..32] of ^Shortint;

Var  a : matrice;
     suma, st : longint;
     n, m, i, j, k : byte;
     s : ^Vector;
     cod1 :Boolean;

Function sumaMatrix : Longint;
Var i, j, aux : integer;
    sm : Longint;
Begin
     sm := 0;
     For i := 1 to n do
         Begin
              aux := 1;
              If s^[i]^ = 1 then aux := -1;
              For j := 1 to m do
                  if s^[j+n]^ = 1 then sm := sm-aux*a[i,j]
                                  else sm := sm+aux*a[i,j];
         End;
     sumaMatrix := sm;
End;

Begin
     Assign( input, 'flip.in' );
     Reset( input );
            Readln( n, m );
            For i := 1 to n do
                For j := 1 to m do
                    Read( a[i,j] );
     Close( input );
     New( s );
     For i := 1 to n+m+1 do New( s^[i] );

     k := 1;
     suma := 0;
     s^[k]^ := -1;
     Repeat
           Repeat
                 If ( s^[k]^ < 1 ) then
                    Begin
                        cod1 := true;
                        s^[k]^ := s^[k]^ +1;
                    End
                    else cod1 := false;
           Until ( not cod1 ) or ( ( cod1 ) and ( k <= m+n ) );
           if cod1 then
              if k = m+n then
                Begin
                    st := sumaMatrix;
                    if st > suma then suma := st;
                End
                else
                Begin
                    k := k+1;
                    s^[k]^ := -1;
                End
              else
              k := k-1;
     Until k = 0;

     Assign( output, 'flip.out' );
     Rewrite( output );
              Writeln( suma );
     Close( output );

     for k := 1 to n+m+1 do
        dispose( s^[k] );
     dispose( s );
End.