Cod sursa(job #607253)

Utilizator RaileanuCristian Raileanu Raileanu Data 11 august 2011 13:28:35
Problema Jocul Flip Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.57 kb
Program flip ;
var a,a1: array[1..5,1..3] of longint;
    b: array[1..32] of byte;
      n,m,i,j,s : longint;
      f1,f2:text;

Procedure citire;
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);
end;
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 increment;
var i,t,k,max: longint;
Begin
     max:=1;
     for i:=1 to n+m do
         max:=max*2;
     for k:=1 to max do Begin
     i:=n+m;
     t:=1;
     while (t=1) and (i>=1) do
           Begin
                b[i]:=b[i]+1;
                if b[i]>1 then Begin b[i]:=0; t:=1; end
                   else t:=0;
                dec(i);
           end;
     suma;
     end;
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
     citire;
     increment;
     rewrite(f2);
     writeln(f2,s);
     close(f2);
end.