Cod sursa(job #1093709)

Utilizator vasi30Axinte Vasilica vasi30 Data 28 ianuarie 2014 15:10:03
Problema Jocul Flip Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.65 kb
program flip;
type matrice=array[1..16,1..16] of integer;
var a:matrice;n,m:byte;
    f,g:text;

function suma_liniep(x:matrice;l,c:byte):longint;
var j:byte;s:longint;
begin
  s:=0;
  for j:=1 to c do
   if(x[l,j]>0) then s:=s+x[l,j];
  suma_liniep:=s;
end;
function suma_coloanap(x:matrice;l,c:byte):longint;
var i:byte;s:longint;
begin
  s:=0;
  for i:=1 to l do
    if (x[i,c]>0) then s:=s+x[i,c];
  suma_coloanap:=s;
end;
function suma_linien(x:matrice;l,c:byte):longint;
var j:byte;s:longint;
begin
  s:=0;
  for j:=1 to c do
    if (x[l,j]<0) then s:=s+x[l,j];
  suma_linien:=s;
end;
function suma_coloanan(x:matrice;l,c:byte):longint;
var i:byte;s:longint;
begin
  s:=0;
  for i:=1 to l do
    if (x[i,c]<0) then s:=s+x[i,c];
  suma_coloanan:=s;
end;

procedure citireMatrice(var x:matrice;var l:byte;var c:byte);
var i,j:byte;
begin
  read(f,l,c);
  readln(f);
  for i:=1 to l do
    for j:=1 to c do read(f, a[i,j]);
end;

procedure flipl(var x:matrice;l,c:byte);
var j:byte;
begin
  for j:=1 to c do x[l,j]:=-x[l,j];
end;

procedure flipc(var x:matrice;l,c:byte);
var i:byte;
begin
  for i:=1 to l do x[i,c]:=-x[i,c];
end;

procedure compar(var x:matrice;l,c:byte);
var i,j:byte;s:longint;
begin
  s:=0;
  for i:=1 to l do
    if suma_liniep(x,i,c)<abs(suma_linien(x,i,c)) then flipl(x,i,c);
  for j:=1 to c do
    if suma_coloanap(x,l,j)<abs(suma_coloanan(x,l,j)) then flipc(x,l,j);

  for i:=1 to l do
    for j:=1 to c do s:=s+x[i,j];
  write(g,s);
end;
begin
  assign(f,'flip.in');
  assign(g,'flip.out');
  reset(f);rewrite(g);
  citireMatrice(a,n,m);
  close(f);
  compar(a,n,m);
  close(g);
end.