Pagini recente » Cod sursa (job #751923) | Cod sursa (job #760430) | Cod sursa (job #2319458) | Cod sursa (job #1104697) | Cod sursa (job #99378)
Cod sursa(job #99378)
{Jocul FLIP. www.InfoArena.pas}
type VECTOR = array[1..16] of longint;
MATRICE = array[1..16] of VECTOR;
var n,m : byte;
A : MATRICE;
L : VECTOR; {sumele pe linii}
C : VECTOR; {sumele pe coloane}
smax : longint;
procedure Citire;
var i,j : byte;
f : text;
begin
Assign(f,'flip.in'); Reset(f);
Readln(f,n,m);
for i:=1 to n do L[i]:=0;
for j:=1 to m do C[j]:=0;
smax := 0;
for i:=1 to n do
for j:=1 to m do
begin
Read(f,A[i,j]);
L[i] := L[i] + A[i,j];
C[j] := C[j] + A[i,j];
smax := smax + A[i,j];
end;
Close(f);
end;
procedure AflaMinimNegativ(var min:longint; var unde, p : byte);
var i, j : byte;
{unde : byte; 1 => in L; 2 => in C}
{p : byte; pozitia minimului}
begin
min := MaxLongInt;
{trec pe la linii}
for i:=1 to n do if L[i] < min then
begin
min:=L[i]; p:=i; unde:=1;
end;
{trec pe la coloane}
for j:=1 to m do if C[j] < min then
begin
min:=C[j]; p:=j; unde:=2;
end;
case unde of
1 : L[p] := -L[p];
2 : C[p] := -C[p];
end;
end;
procedure CorecteazaLiniile(p:byte);
var i : byte;
s : longint;
begin
S:=0;
for i:=1 to n do
begin
L[i]:=L[i] - 2 * A[i,p];
S := S + L[i];
A[i,p]:=-A[i,p];
end;
if S > Smax then Smax := s;
end;
procedure CorecteazaColoanele(p:byte);
var s : longint; j:byte;
begin
S:=0;
for j:=1 to m do
begin
C[j]:=C[j] - 2 * A[p,j];
S := S + C[j];
A[p,j]:=-A[p,j];
end;
if S > Smax then Smax := s;
end;
var min : longint;
unde, p : byte;
f : text;
begin
Citire;
repeat
AflaMinimNegativ(min, unde, p);
if min < 0 then
case unde of
1 : {pe linie} CorecteazaColoanele(p);
2 : {pe coloana} CorecteazaLiniile(p);
end;
until min>=0;
Assign(f,'flip.out'); ReWrite(f); Write(f,smax); Close(f);
end.