Cod sursa(job #35157)

Utilizator Bluedrop_demonPandia Gheorghe Bluedrop_demon Data 21 martie 2007 21:08:26
Problema Jocul Flip Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.53 kb
{
    Jocul Flip
    Versiunea 3.00
}

Program Flip;

Type matrice = array[1..16,1..16] of Longint;

Var a : matrice;
    n, m, i, j : Byte;
    suma, s1, s2 : Longint;

Function si( i : integer ) : Longint;
Var s : Longint;
    j : Integer;
Begin
    s := 0;
    For j := 1 to m do
        s := s+a[i,j];
    si := s;
End;

Function sj( j : integer ) : Longint;
Var s : Longint;
    i : Integer;
Begin
    s := 0;
    For i := 1 to n do
        s := s+a[i,j];
    sj := s;
End;

Procedure InvL( i : Integer );
Var j : Integer;
Begin
    For j := 1 to m do
        a[i,j] := -a[i,j];
End;

Procedure InvC( j : Integer );
Var i : Integer;
Begin
    For i := 1 to n do
        a[i,j] := -a[i,j];
End;

Begin
    Assign( input, 'flip.in' );
    Reset( input );
        Readln( n, m );
        For i := 1 to n do
            Begin
                For j := 1 to m do
                    Read( a[i,j] );
                Readln;
            End;
    Close( input );
    For i := 1 to n do
        For j := 1 to m do
            Begin
                s1 := si( i );
                s2 := sj( j );
                if ( -s1 > s2 ) and ( -s1 > -s2 ) then
                    InvL( i );
                if ( -s2 > -s1 ) and ( -s2 > s1 ) then
                    InvC( j );
            End;

    suma := 0;
    For i := 1 to n do
        For j := 1 to m do
            suma := suma + a[i,j];

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