Cod sursa(job #601043)

Utilizator DarkWishMasterCebotari Vlad DarkWishMaster Data 4 iulie 2011 18:30:26
Problema Jocul Flip Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.39 kb
type  matrice=array[1..16,1..16] of longint;
var a:matrice; x:char; y,max,N,M:int64; F:text; i,j:byte;
 procedure CautaCandidat;
 var max,neg,pos:int64; i,j:byte;
  begin
   max:=0;
   for j:=1 to M do begin
    neg:=0; pos:=0;
      for i:=1 to N do
      if a[i,j]<0 then neg:=neg+a[i,j]
       else pos:=pos+a[i,j];
    if abs(neg)-abs(pos)>max then
     begin
       x:='c';
       y:=j;
       max:=abs(neg)-abs(pos);
     end;
   end;
     for i:=1 to N do  begin
    neg:=0;    pos:=0;
      for j:=1 to M do
      if a[i,j]<0 then neg:=neg+a[i,j]
       else pos:=pos+a[i,j];
    if abs(neg)-abs(pos)>max then
     begin
       x:='l';
       y:=i;
       max:=abs(neg)-abs(pos);
     end;
   end;
 end;
 procedure Flip(x:char; y:integer);
 var i,j:byte;
  begin
   if x='l' then
    for j:=1 to M do
     a[y,j]:=a[y,j]*-1;
   if x='c' then
    for i:=1 to N do
     a[i,y]:=a[i,y]*-1;
 end;
 function  Suma(a:matrice):int64;
 var i,j:integer; S:int64;
  begin
  S:=0;
   for i:=1 to N do
    for j:=1 to M do
     S:=S+a[i,j];
   Suma:=S;
  end;
Begin
Assign(F, 'flip.in');
 reset(F);
 readln(F, N,M);
 for i:=1 to N do
  begin
    for j:=1 to M do
    read(F, a[i,j]);
 readln(F);
 end;
 close(F);
 for i:=1 to N*M do begin
 CautaCandidat;
 Flip(x,y);
 end;
 assign(F, 'Flip.out');
 rewrite(F);
writeln(F, max);
close(F);
readln;
end.