Cod sursa(job #640791)

Utilizator Adr3nal1nStefan Damian Adr3nal1n Data 26 noiembrie 2011 15:13:32
Problema Jocul Flip Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.96 kb
  var i,j,m,n,a,k,s,sum:integer;
  st:array[1..32] of integer;
        t:array[1..16,1..16] of integer;
        as:boolean;
        f:text;
        procedure init;
        begin
        st[k]:=-1;
        end;
                procedure succesor;
                begin
                as:=false;
                if st[k]<1 then begin
                st[k]:=st[k]+1;
                as:=true;
                end;
                end;
                procedure valid;
                begin
                if (st[k]=1) and (k<=m) then
                for i:=1 to n do
                t[k,i]:=t[k,i]*(-1);
                        if (st[k]=1) and (k>m) then
                        for i:=1 to m do
                        t[i,k-m]:=t[i,k-m]*(-1);
                        end;
                        function solutie:boolean;
                        begin
                        solutie:=(k=m+n);
                        end;
                                procedure calc;
                                begin
                                s:=0;
                                for i:=1 to m do
                                for j:=1 to n do
                                s:=s+t[i,j];
                                if s>sum then
                                sum:=s;
                                end;

     begin
     assign(f,'filip.in');
     reset(f);
     read(f,m,n);
     readln(f);
     for i:=1 to m do  begin
     for j:=1 to n do begin
     read(f,t[i,j]);
     end;
     readln(f);
     end;
     k:=1;
     init;
      while k>0 do begin
      repeat
      succesor;
      if as then valid
      until (as) or (not as);
      if as then
        if solutie then begin calc;  end
        else begin
        k:=k+1;
        init; end
        else k:=k-1;
        end;
        assign(f,'flip.out');
        rewrite(f);
        write(f,sum);
        close(f)
        end.

                                end;