Cod sursa(job #254495)

Utilizator thebest001Neagu Rares Florian thebest001 Data 7 februarie 2009 12:28:55
Problema Kdrum Scor 0
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 1.22 kb
#include <stdio.h>
#include <stdlib.h>
struct fact
{
	int numar;
	int exp;
};
int a[51][51];
int dx[]={0,0 ,1,0,-1};
int dy[]={0,-1,0,1, 0};
int main()
{
	freopen("kdrum.in","r",stdin);
	freopen("kdrum.out","w",stdout);
	long m,n,k,xx,XX,yy,YY;
	scanf("%ld %ld %ld\n",&n,&m,&k);
	scanf("%ld %ld %ld %ld",&xx,&yy,&XX,&YY);
	long x,i,j,ss[51][51]={0},aa[51][51]={0};
	for (i=1;i<=n;i++)
		for (j=1;j<=m;j++)
		{
			scanf("%ld",&a[i][j]);
		}
	struct rr
	{
		int x,y;
	} rr[1001];
	rr[1].y=yy;
	rr[1].x=xx;
	int st=0,dr=1;//,gasit=0,cat=0;
	for (j=0;j<=m;j++) for (i=0;i<=n;i++)	aa[i][j]=1;
//	a[1][1]=-1;
	while (st<=dr)
	{
		st++;
		for (i=1;i<=4;i++)
			if (a[ rr[st].y+dy[i] ] [ rr[st].x+dx[i] ]>0 && ss[ rr[st].y+dy[i] ] [ rr[st].x+dx[i] ]==0)
			{
				rr[++dr].x=rr[st].x+dx[i];
				rr[dr].y=rr[st].y+dy[i];
				ss[rr[dr].y][rr[dr].x]=ss[rr[st].y][rr[st].x]+1;
				//acum pun cu K
				aa[rr[dr].y][rr[dr].x]=(aa[rr[dr].y-1][rr[dr].x]*a[rr[dr].y][rr[dr].x]*aa[rr[dr].y][rr[dr].x-1])%k;
				if (aa[rr[dr].x][rr[dr].y]==0 && rr[dr].x==XX && rr[dr].y==YY)
				{
					printf("%ld",ss[rr[dr].x][rr[dr].y]+1);
					return 0;
				}
			}
			if (rr[st].x==1 && rr[st].y==1)
			ss[1][1]=-1;
	}
	return 0;
}