Cod sursa(job #59937)

Utilizator RobybrasovRobert Hangu Robybrasov Data 11 mai 2007 12:42:24
Problema Jocul Flip Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.25 kb
type matrice=array[1..16,1..16] of integer;
     vector=array[1..16] of integer;
var a:matrice;
    i,s,smax,j,m,n,ncl,ncc:integer;
    nl,nc,kl,kc,x:longint;
    ll,cc:vector;
    f:text;

procedure adunare(var v:vector; var nc:integer);
var i,t,s:integer;
begin
  s:=v[1]+1;
  v[1]:=s mod 2;
  t:=s div 2;
  i:=2;
  while (i<=nc) and (t>0) do
    begin
      s:=v[i]+t;
      v[i]:=s mod 2;
      t:=s div 2;
      inc(i);
    end;
  if t=1 then
    begin
      inc(nc);
      v[nc]:=1;
    end;
end;


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);
  s:=0;
  for i:=1 to n do
    for j:=1 to m do
      inc(s,a[i,j]);
  smax:=s;
  nl:=1;nc:=1;
  nl:=(nl shl n) -1 ;
  nc:=(nc shl m) -1 ;
  ncl:=1;
  for kl:=0 to nl do
    begin
      ncc:=1;
      for kc:=0 to nc do
        begin
          s:=0;
          for i:=1 to n do
          for j:=1 to m do
          if (ll[i]+cc[j]=0)or(ll[i]+cc[j]=2)then s:=s+a[i,j]
          else s:=s-a[i,j];

           if s>smax then smax:=s;
           adunare(cc,ncc);
         end;
        adunare(ll,ncl);
     end;
  assign(f,'flip.out');
  rewrite(f);
  write(f,smax);
  close(f);
end.