Cod sursa(job #607180)

Utilizator RaileanuCristian Raileanu Raileanu Data 10 august 2011 23:26:33
Problema Jocul Flip Scor 40
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.12 kb
Program flip ;
var a,a1: array[1..16,1..16] of longint;
    b: array[1..32] of 0..1;
      n,m,i,j,s : longint;
      f1,f2:text;

Procedure suma;
var i,j: byte;
    sm: longint;
Begin
     a1:=a;
     for i:=1 to n do
         if b[i]=1 then
            for j:=1 to m do
                a1[i,j]:=a1[i,j]* -1;
     for i:=n+1 to n+m do
         if b[i]=1 then
            for j:=1 to n do
                a1[j,i-n]:=a1[j,i-n]* -1;
    sm:=0;
    for i:=1 to n do
        for j:=1 to m do
            sm:=sm+a1[i,j];
    if sm>s then s:=sm;
end;

Procedure back(k: byte);
var cont,i: byte;
Begin
     if k=n+m+1 then suma
        else
            for i:=0 to 1 do
                Begin
                b[k]:=i;
                cont:=1;
                if cont=1 then
                   back(k+1);
                end;
end;

Begin
     assign(f1, 'flip.in');
     assign(f2, 'flip.out');
     reset(f1);
     readln(f1,n,m);
     for i:=1 to n do
         for j:=1 to m do
             read(f1,a[i,j]);
     close(f1);
     back(1);
     rewrite(f2);
     writeln(f2,s);
     close(f2);
end.