Cod sursa(job #1552887)

Utilizator ili226Vlad Ilie ili226 Data 18 decembrie 2015 21:09:52
Problema Jocul Flip Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.91 kb
var f:text;
    k,n,m,i,j,l,o,r,c:byte;
    sch:array[1..3]of record
                       lc,care:byte;
                      end;
    a:array[1..16,1..16]of longint;
    s,sc,max:longint;
    pot_iesi:boolean;
begin
randomize;
assign(f,'flip.in');
reset(f);s:=0;
readln(f,n,m);
for i:=1 to n do
 for j:=1 to m do
  begin
   read(f,a[i,j]);
   inc(s,a[i,j])
  end;
close(f);
max:=-maxlongint;
r:=0;c:={random(10)+1}100;
repeat
pot_iesi:=false;k:=0;
{caut linia cu flip maxim}
for i:=1 to n do
 begin
  sc:=s;
  for j:=1 to m do
   sc:=sc-a[i,j]+(a[i,j]*(-1));
  if sc>max then
   begin
    max:=sc;
    k:=1;
    sch[k].lc:=0;
    sch[k].care:=i;
   end;
 end;
{caut coloana cu flip maxim}
for j:=1 to m do
 begin
  sc:=s;
  for i:=1 to n do
   sc:=sc-a[i,j]+(a[i,j]*(-1));
  if sc>max then
   begin
    max:=sc;
    k:=1;
    sch[k].lc:=1;
    sch[k].care:=j;
   end;
 end;
{caut combinatia linie-coloana cu flip maxim}
for i:=1 to n do
 for j:=1 to m do
  begin
   sc:=s;
   for l:=1 to m do
     sc:=sc-a[i,l]+(a[i,l]*(-1));
   for l:=1 to n do
     sc:=sc-a[l,j]+(a[l,j]*(-1));
   sc:=sc+4*(a[i,j]);
   if sc>max then
    begin
     max:=sc;
     k:=1;
     sch[k].lc:=0;
     sch[k].care:=i;
     inc(k);
     sch[k].lc:=1;
     sch[k].care:=j;
    end;
  end;
if r=c then pot_iesi:=true;
if max<=s then {pot_iesi:=true}inc(r);
          {else}
           begin
            s:=max;
            for i:=1 to k do
             case sch[i].lc of
              0:
               begin
                for j:=1 to m do
                  a[sch[i].care,j]:=-a[sch[i].care,j]
               end;
              1:
               begin
                for j:=1 to n do
                 a[j,sch[i].care]:=-a[j,sch[i].care]
               end;
             end;
           end;
until pot_iesi;
assign(f,'flip.out');

rewrite(f);
writeln(f,max);
close(f);
end.