Cod sursa(job #189854)

Utilizator FllorynMitu Florin Danut Flloryn Data 18 mai 2008 18:39:53
Problema Jocul Flip Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.44 kb
  program flip;  
    var f,g:text;  
       a:array[1..16,1..16]of longint;  
       b:array[0..16]of byte;  
       s1,s2,max:longint;  
       x,i,j,n,m,aux:integer;  
   begin  
   assign(f,'flip.in');reset(f);  
   assign(g,'flip.out');rewrite(g);  
    readln (f,n,m);  
     for i:=1 to n do  
     begin  
     for j:=1 to m do read (f,a[i,j]);  
    readln(f);  
     end;  
      x:=1;  
      max:=0;   
      while x>0 do  
       begin  
       if b[x]<2 then b[x]:=b[x]+1;  
       if x=n then begin  
                 s2:=0;  
                 j:=1;  
                 while j<=m do begin  
                        s1:=0;  
                               i:=1;  
                        while i<=n do  
                    begin  
                                     if b[i]=2 then s1:=s1-a[i,j]  
                    else s1:=s1+a[i,j];  
                                    i:=i+1;  
                           end;  
                   if s1<aux then s2:=s2-s1  
                    else s2:=s2+s1;  
                      j:=j+1;  
                               end;  
             if s2>max then max:=s2;  
             while b[x]=2 do  
                        begin  
            x:=x-1;  
           end;  
          end  
             else begin  
                  x:=x+1;  
                  b[x]:=0;  
                  end;  
      end;  
 writeln(g,max);  
 close(f);  
 close(g);     
 end.