Cod sursa(job #214890)

Utilizator lianaliana tucar liana Data 16 octombrie 2008 17:26:08
Problema Jocul Flip Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.41 kb
program flip;
var n, m, i, j, s, st:longint;
    f, g:text;
    a, x:array[1..16,1..16] of longint;
    gasit:boolean;
  begin
    assign(f,'flip.in'); reset(f);
    assign(g,'flip.out'); rewrite(g);
    readln(f, n, m);
    gasit:=true;
    while gasit do
      begin
        gasit:=false;
        for i:=1 to n do
          begin
            s:=0;
            for j:=1 to m do
              begin
                read(f,a[i,j]);
                s:=s+a[i,j];
                st:=st+a[i,j];
              end;
            if s<0 then
              begin
               gasit:=true;
              { for j:=1 to m do
                 begin
                   if x[i,j]<>1 then
                     a[i,j]:=0-a[i,j];
                   x[i,j]:=1;
                 end;}
              st:=st+2*(0-s);
             end;
          end;
        for j:=1 to m do
          begin
            s:=0;
            for i:=1 to n do
              begin
                s:=s+a[i,j];
              end;
            if s<0 then
              begin
                gasit:=true;
                st:=st+2*(0-s);
                {for i:=1 to n do
                  begin
                    if x[i,j]<>1 then
                      a[i,j]:=0-a[i,j];
                    x[i,j]:=1;
                  end;       }
              end;
          end;
      end;
    writeln(g,st);
    close(f);
    close(g);
  end.