Cod sursa(job #333129)

Utilizator sapiensCernov Vladimir sapiens Data 21 iulie 2009 15:50:35
Problema Jocul Flip Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.41 kb
Program Flip;
 var f,g:text; a:array[1..16,1..16]of -1000000..1000000;
     m,n:1..16; smax:longint;
 procedure initiere;
  var x,y:1..16;
  begin
   assign (f,'flip.in'); reset (f);
   assign (g,'flip.out'); rewrite (g);
   readln (f,n,m);
   for x:=1 to n do begin
     for y:=1 to m do read (f,a[x,y]);
     readln (f);
   end;
   smax:=-maxlongint-1;
  end;
 function lsum (x:byte):longint;
  var y:byte; z:longint;
  begin
   z:=a[x,1];
   for y:=2 to m do z:=z+a[x,y];
   lsum:=z;
  end;
 function csum (x:byte):longint;
  var y:byte; z:longint;
  begin
   z:=a[1,x];
   for y:=2 to n do z:=z+a[y,x];
   csum:=z;
  end;
 procedure incheiere;
  begin
   writeln (g,smax);
   close (f); close (g);
  end;
 procedure lcom (x:byte);
  var y:byte;
  begin
   for y:=1 to m do a[x,y]:=-a[x,y];
  end;
 procedure ccom (x:byte);
  var y:byte;
  begin
   for y:=1 to n do a[y,x]:=-a[y,x];
  end;
 function sum:longint;
  var x,y:byte; z:longint;
  begin
   z:=0;
   for x:=1 to n do for y:=1 to m do z:=z+a[x,y];
   sum:=z;
  end;
 procedure cautare;
  var x:byte;
  begin
   if sum>smax then smax:=sum;
   for x:=1 to n do
     if lsum (x)<0 then begin
       lcom (x);
       cautare;
       lcom (x);
     end;
   for x:=1 to m do
     if csum (x)<0 then begin
       ccom (x);
       cautare;
       ccom (x);
     end;
  end;
 begin
  initiere;
  cautare;
  incheiere;
 end.