# Cod sursa(job #254295)

Utilizator Data 7 februarie 2009 10:50:34 Kdrum 0 cpp done Stelele Informaticii 2009, clasele 9-10, ziua 2 1.05 kb
``````#include <stdio.h>
#define DMax 60
#define CMax 100000

typedef struct
{
int x,y,dist;
long produs;
}tip;

long a[DMax][DMax];
int n,m,k,x1,x2,y1,y2,dx[]={-1,0,1,0},dy[]={0,1,0,-1};
tip C[CMax];

void citire();
int drum();

int main()
{
freopen("kdrum.in","r",stdin);
freopen("kdrum.out","w",stdout);
citire();
printf("%d\n",drum());
return 0;
}

void citire()
{
scanf("%d %d %d\n",&n,&m,&k);
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%ld",&a[i][j]);
}

int drum()
{
long ic,sc,i;
tip x;
ic=1;
sc=1;
C[ic].x=x1;
C[ic].y=y1;
C[ic].dist=1;
C[ic].produs=a[x1][y1];
while(ic<=sc)
{
x=C[ic];
ic++;
for(i=0;i<=3;i++)
if(a[x.x+dx[i]][x.y+dy[i]]!=0)
{
sc++;
C[sc].x=x.x+dx[i];
C[sc].y=x.y+dy[i];
C[sc].dist=x.dist+1;
C[sc].produs=x.produs*a[x.x+dx[i]][x.y+dy[i]];
if((C[sc].x==x2) && (C[sc].y==y2) && (C[sc].produs%k==0))
return C[sc].dist;
}
}
return -1;
}
``````