Pagini recente » Cod sursa (job #2896083) | Cod sursa (job #793704) | Cod sursa (job #923482) | Cod sursa (job #2863983) | Cod sursa (job #4181)
Cod sursa(job #4181)
program flip;
var m,n,i,j:byte;
a:array[1..16,1..16] of longint;
x:array[1..32]of integer;
f,h:text;
function cond(k:integer):boolean;
var i:byte;
s:longint;
begin
s:=0;
if k<=m then
for i:=1 to n do s:=s+a[i,k]
else for i:=1 to m do
if x[i]=0 then s:=s+a[k-m,i]
else s:=s-a[k-m,i];
if x[k]=1 then s:=s*(-1);
cond:=s>=0;
end;
function sol(k:integer):boolean;
begin
sol:=k=m+n;
end;
procedure tipar;
var s:longint;
i,j:integer;
begin
s:=0;
for i:=1 to n do
for j:=1 to m do
if ((x[j]=1)and(x[m+i]=1))or
((x[j]=0)and(x[m+i]=0))
then s:=s+a[i,j]
else s:=s-a[i,j];
writeln(h,s);
end;
procedure back;
var k:integer;
begin
k:=1;x[k]:=-1;
while k>0 do
if x[k]<1 then
begin
x[k]:=x[k]+1;
if cond(k) then
if sol(k) then
begin
tipar;
k:=-1;
end
else begin
inc(k);
x[k]:=-1;
end;
end
else dec(k);
end;
begin
assign(f,'flip.in');
assign(h,'flip.out');
reset(f);rewrite(h);
readln(f,n,m);
for i:=1 to n do
for j:=1 to m do read(f,a[i,j]);
back;
close(f);close(h);
end.