Cod sursa(job #18559)

Utilizator petrePajarcu Alexandru-Petrisor petre Data 18 februarie 2007 12:37:41
Problema Plantatie Scor 0
Compilator fpc Status done
Runda preONI 2007, Runda 2, Clasa a 10-a Marime 1.8 kb
const
 nmax=500;

var
 n,st,dr,sus,jos:longint;
 mat,c:array[0..nmax+1,0..nmax+1] of longint;

procedure refresh(indx,indy:integer);
var
 pi,pj,val,i,j:longint;
begin
 pi:=1;
 val:=mat[indx,indy];
 while indx<=n do begin
   pj:=1;
   j:=indy;
   while j<=n do begin
     if val>c[indx,j] then
      c[indx,j]:=val;
     while j and pj=0 do
       pj:=pj*2;
     j:=j+pj;
   end;
   while indx and pi=0 do
     pi:=pi*2;
   indx:=indx+pi;
 end;
end;

function calcul(x,y:longint):longint;
var
 pi,pj,i,j,max:longint;
begin
 max:=0;
 pi:=1;
 while x>0 do begin
   pj:=1;
   j:=y;
   while j>0 do begin
     if (c[x,j]>max) then
       max:=c[x,j];
      while j and pj=0 do
        pj:=pj*2;
     j:=j-pj;
   end;
   while x and pi=0 do
     pi:=pi*2;
   x:=x-pi;
  end;
 calcul:=max;
end;

function rezultat(a,b,c,d:longint):longint;
var
 x: array [0..4] of longint;
 max,i:longint;
begin
 x[1]:=a;
 x[2]:=b;
 x[3]:=c;
 x[4]:=d;
 max:=x[1];
 for i:=2 to 4 do
  if x[i]>max then max:=x[i];
 rezultat:=max;
end;

procedure max;
var
 x,y,max1,max2,max3,max4:longint;
begin
 x:=jos;  y:=dr;
 max1:=calcul(x,y);

 x:=jos;  y:=st-1;
 max2:=calcul(x,y);

 x:=sus-1; y:=dr;
 max3:=calcul(x,y);

 x:=sus-1; y:=dr-1;
 max4:=calcul(x,y);

 writeln(rezultat(max1,max2,max3,max4));
end;

procedure citire;
var
 i,j,m,x,y,k:longint;
begin
 assign(input,'plantatie.in');reset(input);
 assign(output,'plantatie.out');rewrite(output);
 readln(n,m);

 for i:=1 to n do
  for j:=1 to n do
   c[i,j]:=0;

 for i:=1 to n do
  for j:=1 to n do begin
   read(mat[i,j]);
   refresh(i,j);
  end;

 for i:=1 to m do begin

  read(x,y,k);
  sus:=x;
  jos:=x+k-1;
  st:=y;
  dr:=y+k-1;
  max;

 end;
 close(input); close(output);
end;

begin
 citire;
end.