Cod sursa(job #468275)

Utilizator lianaliana tucar liana Data 2 iulie 2010 22:08:58
Problema Jocul Flip Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.55 kb
program jocul_flip;
var f, g:text;
    ma:array[0..16,0..16] of longint;
    max, i, j, n, m, sumcol, stot:longint;
    sol:array[1..100] of longint;

procedure citire;
  begin
    readln(f,n,m);
    for i:=1 to n do
      begin
        for j:=1 to m do
          begin
            read(f,ma[i,j]);
    {        if ma[i,j]<0 then
              begin
                lneg[i]:=lneg[i]+ma[i,j];
                cneg[j]:=cneg[j]+ma[i,j];
              end
             else
               begin
                 lpoz[i]:=lpoz[i]+ma[i,j];
                 cpoz[j]:=cpoz[j]+ma[i,j];
               end;}
          end;
      end;
  end;

procedure calculare;
  begin
    stot:=0;
    for j:=1 to m do
      begin
        sumcol:=0;
        for i:=1 to n do
          if sol[i]=0 then
            sumcol:=sumcol+ma[i,j]
           else
             sumcol:=sumcol-ma[i,j];
        if sumcol<0 then
          sumcol:=-sumcol;
        stot:=stot+sumcol;
      end;
    if stot>max then
      max:=stot;
  end;

procedure gen(k:longint);
  begin
    if k=n+1 then
      calculare
     else
       begin
         sol[k]:=0;
{         s:=s+lpoz[i]+lneg[i];}
         gen(k+1);
{         s:=s-(lpoz[i]+lneg[i]);}

         sol[k]:=1;
 {        s:=s-(lpoz[i]+lneg[i]);}
         gen(k+1);
{         s:=s+(lpoz[i]+lneg[i]);  }
       end;
  end;

  begin
    assign(f,'flip.in'); reset(f);
    assign(g,'flip.out'); rewrite(g);
    citire;
    max:=-maxlongint;
    gen(1);
    writeln(g,max);
    close(f);
    close(g);
  end.