Cod sursa(job #1996477)

Utilizator _Victor_Victor Ciobanu _Victor_ Data 1 iulie 2017 17:38:00
Problema Jocul Flip Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.76 kb
Program Flip;
type max = 1..16;
tab = array[max,max] of -1000000..1000000;
var n,m,i,j:max;
fl:tab;
stack: array[max] of 0..1;
a,s:longint;
f,g:text;
function Solutie:longint;
var i,j:max;
s:longint;
begin
 s:=0;
 for i:=1 to n do begin
  for j:=1 to m do
   if stack[j] = 0 then
    s:=s+fl[i,j]
    else
    s:=s-fl[i,j];
 end;
end;
procedure backtrack(k:max);
var x: longint;
begin
 if k > m then begin
  x:= solutie;
  if x > s then s:= x;
  exit;
  end;
stack[k]:=0;
backtrack(k+1);
stack[k]:=1;
backtrack(k+1);
end;
begin
assign(f,'flip.in');assign(g,'flip.out');
reset(f);rewrite(g);
readln(f,n,m);
for i:=1 to n do begin
for j:=1 to m do
 read(f,fl[i,j]);
readln(f);
end;
Backtrack(1);
writeln(g,s);
close(f);close(g);
end.