Cod sursa(job #183364)

Utilizator victor_bla_blaDumitrescu Victor victor_bla_bla Data 21 aprilie 2008 23:42:09
Problema Teren Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.87 kb
program teren;
type intr=0..900;
var a: array[0..6,0..6] of intr; {!!!!!}
vm: array[1..4] of integer;
n,m,i,j,k,x,ip,jp,s,amax,ar: integer;
f: text;
{procedure assi;
begin
     vm[1]:=i;
     vm[2]:=j;
     vm[3]:=ip;
     vm[4]:=jp;
end;}
begin
     assign(f,'teren.in');
     reset(f);
     readln(f,n,m,x);
     for i:=1 to n do
     begin
          for j:=1 to m do
          begin
               read(f,k);
               a[i,j]:=a[i-1,j]+a[i,j-1]-a[i-1,j-1]+k;
          end;
          readln(f);
     end;
     close(f);
     assign(f,'teren.out');
     rewrite(f);
     if x>a[n,m] then
     begin
          write(f,n*m);
          close(f);
          exit;
     end;
     i:=1;j:=1;ip:=1;jp:=1;s:=a[i,j];
     while (ip<n) or (jp<m) do
     begin
               while (x>=s) and ((ip<n) or (jp<m)) do
               begin
                    if ip<n then inc(ip);
                    s:=a[ip,jp]-a[ip,j-1]-a[i-1,jp]+a[i-1,j-1];
                    ar:=(ip-i+1)*(jp-j+1);
                    if jp<m then inc(jp);
                    if (ar>amax) and (s<=x) then amax:=ar;
                    s:=a[ip,jp]-a[ip,j-1]-a[i-1,jp]+a[i-1,j-1];
                    ar:=(ip-i+1)*(jp-j+1);
                    if (ar>amax) and (s<=x) then amax:=ar;
               end;
               if (x<s) and ((i<ip) or (j<jp)) then
               begin
                    if i<ip then inc(i);
                    s:=a[ip,jp]-a[ip,j-1]-a[i-1,jp]+a[i-1,j-1];
                    ar:=(ip-i+1)*(jp-j+1);
                    if (ar>amax) and (s<=x) then begin amax:=ar;  end;
                    if j<jp then inc(j);
                    s:=a[ip,jp]-a[ip,j-1]-a[i-1,jp]+a[i-1,j-1];
                    ar:=(ip-i+1)*(jp-j+1);
                    if (ar>amax) and (s<=x) then begin amax:=ar;  end;
               end;
     end;
     write(f,amax);
     close(f);
end.