Cod sursa(job #254322)

Utilizator alexandru92alexandru alexandru92 Data 7 februarie 2009 11:11:58
Problema Kdrum Scor 0
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 0.99 kb
#include<stdio.h>
#include<math.h>
const int dx[]={-1,0,1,0};
const int dy[]={0,1,0,-1};
long n,m,k,x,y,fx,fy,lg,lgmax,nr,a[51][51];
long double p;
void Drum(int,int);
int main()
{
	FILE *f=freopen("kdrum.in","rt",stdin),*g=freopen("kdrum.out","wt",stdout);
	scanf("%ld %ld %ld %ld %ld %ld %ld",&n,&m,&k,&x,&y,&fx,&fy);
	lgmax=n*m+1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) scanf("%ld",&a[i][j]);
	Drum(x,y);
	printf("%ld",lgmax);
    return 0;
}
inline int valid(long double p,long k)
{long double p2=floor((long double)(p/k));
  if(p2*k==p) return 1;
  return 0;
}
void Drum(int x,int y)
{nr=a[x][y];//to keep the value of a[x][y]
 a[x][y]=0; p*=nr; lg++; //a[x][y]=0 we have been hear already, the length lg++, p*=a[x][y]; the produse
	if(x==fx&&y==fy) 
	  {	if(valid(p,k))
			if(lgmax>lg) lgmax=lg;
	   }
	  else for(int i=0;i<4;i++)
	     	    if(a[x+dx[i]][y+dy[i]])
		    		Drum(x+dx[i],y+dy[i]);
 a[x][y]=nr;  p/=a[x][y]; lg--; //restore the initial condition
}