Cod sursa(job #89408)

Utilizator taloibogdanTaloi Bogdan Cristian taloibogdan Data 6 octombrie 2007 19:27:06
Problema Jocul Flip Scor 40
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.91 kb
Program Flip;
Var f:text;
    n,m,i,j,max,s,b,ii,b1,i1,j1:longint;
    a:array[1..16,1..16] of longint;
    l,c:array[1..16] of byte;
Begin
  assign(f,'flip.in');
  reset(f);
  readln(f,n,m);
  for i:=1 to n do
     for j:=1 to m do read(f,a[i,j]);
  close(f);
  max:=-maxlongint;
  b:=1 shl n;
  for i:=1 to b do
     begin
      ii:=i;
      for j:=n downto 1 do begin l[j]:=ii mod 2; ii:=ii div 2; if ii=0 then break; end;
      b1:=1 shl m;
      for i1:=1 to b1 do
         begin
          ii:=i1;
          for j:=m downto 1 do begin c[j]:=ii mod 2; ii:=ii div 2; if ii=0 then break; end;
          s:=0;
          for j:=1 to n do
             for j1:=1 to m do
                if l[j]+c[j1]=1 then s:=s+a[j,j1]*-1
                                else s:=s+a[j,j1];
          if s>max then max:=s;
         end;
     end;
  assign(f,'flip.out');
  rewrite(f);
  Writeln(f,max);
  close(f);
End.