Cod sursa(job #135727)

Utilizator bishoppAlex Cristian bishopp Data 14 februarie 2008 12:26:02
Problema Jocul Flip Scor 50
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.63 kb
type matrice=array[1..16,1..16] of longint;
     stiva=array[1..16] of integer;
var a:matrice;
    st:stiva;
    as,ev:boolean;
    f,g:text;
    maxi,maxim:longint;
    n,m,i,j,k:byte;

procedure succesor(var st:stiva;k:integer;var as:boolean);
begin
if st[k]<1 then begin
                inc(st[k]);
                as:=true;
                end
           else as:=false;
end;

function solutie(k:integer):boolean;
begin
solutie:=k=n;
end;

procedure verif(var max:longint);
var s:longint;
    x:byte;
begin
for i:=1 to n do if st[i]=1 then for j:=1 to m do a[i,j]:=-a[i,j];
for j:=1 to m do begin
    s:=0;
    for i:=1 to n do s:=s+a[i,j];
    if -s>s then for x:=1 to n do a[x,j]:=-a[x,j];
    end;

max:=0;
for i:=1 to n do
    for j:=1 to n do max:=max+a[i,j];
end;



begin
assign(f,'flip.in');reset(f);
assign(g,'flip.out');rewrite(g);
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;
k:=1;
st[k]:=-1;
maxi:=-maxint;
while k>0 do begin
             repeat
             succesor(st,k,as);
             until as or not as;
             if as then if solutie(k) then begin
                                           verif(maxim);
                                           if maxim>maxi then maxi:=maxim;
                                           end
                                      else begin
                                           inc(k);
                                           st[k]:=-1;
                                           end
                   else dec(k);
             end;
writeln(g,maxi);
close(f);
close(g);
end.