Cod sursa(job #33317)

Utilizator ScrazyRobert Szasz Scrazy Data 19 martie 2007 09:51:05
Problema Jocul Flip Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.64 kb
var s:array[1..18] of char;
    n,m,i,j,k:byte;
    hatv:longint;
    x:array[1..18] of char;
    f:Text;
    a:array[1..18,1..18] of longint;
    ossz,max:longint;
    oszlop:array[1..18] of longint;

function gyors(a:byte;k:shortint):word;
var x:word;
begin
  if k=1 then gyors:=a
  else begin
    x:=gyors(a,k div 2);
    if odd(k) then gyors:=x*x*a
    else gyors:=x*x;
  end;

end;


procedure general;
var
    i,m:byte;
begin
 m:=0;
 for i:=n downto 1 do
   if x[i]='1' then begin
     if m=0 then begin
       if s[i]='0' then s[i]:='1'
       else if s[i]='1' then begin s[i]:='0';m:=m+1;end;
     end
     else if m>0 then begin
         if s[i]='0' then begin s[i]:='0';end
         else if s[i]='1' then s[i]:='1'
     end;
   end
   else if x[i]='0' then begin
     if m>0 then begin
       if s[i]='0' then begin s[i]:='1';m:=m-1;end
       else if s[i]='1' then s[i]:='0';
     end;
   end;
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-1 do x[i]:='0';
  x[n]:='1';
for i:=1 to n do s[i]:='0';
hatv:=gyors(2,n);

max:=0;
for k:=1 to hatv do begin

  ossz:=0;
  for j:=1 to m do
    for i:=1 to n do
      if s[i]='1' then oszlop[j]:=oszlop[j]-a[i,j]
      else oszlop[j]:=oszlop[j]+a[i,j];

  for j:=1 to m do
    if oszlop[j]<0 then begin ossz:=ossz-oszlop[j];oszlop[j]:=0;end
    else if oszlop[j]>=0 then begin ossz:=ossz+oszlop[j];oszlop[j]:=0;end;

  if ossz>max then max:=ossz;



  general;

end;
assign(f,'flip.out');
rewrite(f);
writeln(f,max);
close(f);
end.