Cod sursa(job #254445)

Utilizator MihaiBunBunget Mihai MihaiBun Data 7 februarie 2009 12:10:33
Problema Kdrum Scor 0
Compilator fpc Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 6.19 kb
program jiji;
var f:text;
    i,j,n,m,k,x1,x2,y1,y2,l,min,b,g,j1:longint;
    st,x,y:array[0..2600] of longint;
    er:array[1..12000] of 0..1;
    viz,ex,p,prim:array[1..12000] of integer;
    a:array[0..51,0..51] of longint;
    as,ev,e:boolean;
begin
   assign(f,'kdrum.in');
   reset(f);
   readln(f,n,m,k);
   readln(f,x1,y1,x2,y2);

   for i:=1 to n do
     for j:=1 to m do
                 read(f,a[i,j]);
  p[1]:=2;l:=1;
  i:=1;
  repeat
      i:=i+2;
      if er[i]=0 then begin
                        l:=l+1;
                        p[l]:=i;
                        j:=i*i;
                        while j<=k do
                            begin
                            er[j]:=1;
                            j:=j+i
                            end;
                      end;
  until i>2000;
  j:=1;
  b:=k;
  g:=0;
  while (p[j]*p[j]<=b)and(b>1) do
         begin
             if b mod p[j]=0 then begin
                                   g:=g+1;
                                   prim[g]:=p[j];
                                   ex[g]:=0;
                                   while (b mod p[j])=0 do begin
                                                           ex[g]:=ex[g]+1;
                                                           b:=b div p[j];
                                                         end;
                                   end;
              j:=j+1
          end;
   if b>1 then begin
                 g:=g+1;
                 prim[g]:=b;
                 ex[g]:=1
               end;
   {INITIALIZEZ VECTORUL DE VIZITARE}
   b:=a[x1,y1];
   j:=1;
     while (p[j]*p[j]<=b)and(b>1) do
         begin
             if b mod p[j]=0 then begin
                                    viz[p[j]]:=0;
                                    while b mod p[j]=0 do begin
                                                           viz[p[j]]:=viz[p[j]]+1;
                                                           b:=b div p[j];
                                                         end;
                                   end;
              j:=j+1
          end;
   if b>1 then viz[b]:=1;
     {BACK..........>>>>}
   min:=100000;
   j:=1;
   st[j]:=0;
   x[0]:=x1;
   y[0]:=y1;
   while j>0 do
   begin
      repeat
          if st[j]<4 then begin
                           st[j]:=st[j]+1;
                           as:=true
                          end
                     else as:=false;
          if as then begin
                       case st[j] of
                        1:begin
                           x[j]:=x[j-1];
                           y[j]:=y[j-1]+1;
                          end;
                        2:begin
                           x[j]:=x[j-1]+1;
                           y[j]:=y[j-1]
                          end;
                        3:begin
                           x[j]:=x[j-1];
                           y[j]:=y[j-1]-1
                          end;
                        4:begin
                           x[j]:=x[j-1]-1;
                           y[j]:=y[j-1]
                         end;
                         end;
                         if a[x[j],y[j]]=0 then ev:=false
                                           else if j>=min then ev:=false
                                                   else
                                                begin
                                                  ev:=true;
                                                  b:=a[x[j],y[j]];
                                                  j1:=1;
                                                  while (p[j1]*p[j1]<=b)and(b>1) do
                                                           begin
                                                            if b mod p[j1]=0 then begin

                                                                                 while b mod p[j1]=0 do begin
                                                                                 viz[p[j1]]:=viz[p[j1]]+1;
                                                                                   b:=b div p[j1];
                                                                                                     end;
                                                                                 end;
                                                           j1:=j1+1
                                                           end;
                                                           if b>1 then viz[b]:=viz[b]+1;
                                                end;
                           end;
      until (not as)or (as and ev);
      if as then if (x[j]=x2)and(y[j]=y2) then
                                begin
                                   e:=true;
                                   for i:=1 to g do
                                     if viz[prim[i]]<ex[i] then e:=false;
                                   if e then if j<min then min:=j
                                end
                                            else begin
                                                  j:=j+1;
                                                  st[j]:=0
                                                 end
            else begin
                   j:=j-1;
                   b:=a[x[j],y[j]];
                   j1:=1;
                   while (p[j1]*p[j1]<=b)and(b>1) do
                                    begin
                                    if b mod p[j1]=0 then begin

                                                           while b mod p[j1]=0 do begin
                                                                              viz[p[j1]]:=viz[p[j1]]-1;
                                                                              b:=b div p[j1];
                                                                                  end;
                                                          end;
                                    j1:=j1+1
                                    end;
                                    if b>1 then viz[b]:=viz[b]-1;
               end;
end;
    close(f);
     assign(f,'kdrum.out');
    rewrite(f);
    writeln(f,min+1);
    close(f);

end.