Cod sursa(job #254489)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 7 februarie 2009 12:25:48
Problema Kdrum Scor 20
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 1.5 kb
/*

*/

#include <cstdio>

int N, M, K, xs, ys, xf, yf, viz[51][51], a[51][51], c[51][51], sol;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

void citire()
{
	freopen("kdrum.in","r",stdin);
	freopen("kdrum.out","w",stdout);

	int i, j;

	scanf("%d %d %d",&N,&M,&K);
	scanf("%d %d %d %d",&xs,&ys,&xf,&yf);

	for (i = 1; i <= N; i++)
		for (j = 1; j <= M; j++)
		{
			scanf("%d", &a[i][j]);
			if (!a[i][j])  a[i][j] = -1;
			c[i][j] = 1;
		}	
	for (i = 1; i <= N; i++) a[i][0] = a[i][M + 1] = -1;
	for (i = 1; i <= M; i++) a[0][i] = a[N + 1][i] = -1;

}

int mod(int x, int y)
{
	int cat, rez;
	cat = x / y;
	rez = x - (cat * y);
	return rez;
}

int min(int x, int y) { return x < y ? x : y;}

struct punct
{
	int x, y;
} C[100000];

void Lee()
{
	int i, p, u, xx, yy, poz;
	p = u = 1;
	C[1].x = xs; C[1].y = ys;
	viz[xs][ys] = 1;

	while (p <= u)
	{
		for (i = 0; i < 4; i++)
		{
			poz = p % 100000;
			xx = C[poz].x + dx[i];
			yy = C[poz].y + dy[i];

			if (a[xx][yy] != -1 && (c[xx][yy] != 0 || ( c[xx][yy] == 0 && viz[xx][yy] > viz[C[poz].x][C[poz].y] + 1)))
			{
				c[xx][yy] = mod (c[C[poz].x][C[poz].y] * a[xx][yy], K);
				viz[xx][yy] = viz[C[poz].x][C[poz].y] + 1;
				if (!c[xx][yy] && xx == xf && yy == yf) sol = min(sol, viz[xx][yy]);
				u++;
				C[u % 100000].x = xx;
				C[u % 100000].y = yy;
			}
		}
		p++;
	}
}






int main()
{
	citire();
	
	sol = 100000000;
	Lee();
	printf("%d\n",sol);

	return 0;
}