Cod sursa(job #254255)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 7 februarie 2009 03:54:14
Problema Kdrum Scor Ascuns
Compilator cpp Status done
Runda Marime 0.97 kb
#include <stdio.h>
#include <string.h>

#define MAXN 60
#define MAXN 60
#define MAXX 2510
#define MAXL 4

int N, M, K, L;
int x1, y1, x2, y2;
int X[MAXX], Y[MAXX];
int A[MAXN][MAXN], V[MAXN][MAXN];
int dirx[MAXL] = {-1, 1, 0, 0};
int diry[MAXL] = {0, 0, -1, 1};

int BFS(int x1, int y1, int x2, int y2)
{
	int i, j, cx, cy;

	memset(V, -1, sizeof(V));

	L = 1;
	V[x1][y1] = 1;
	X[L] = x1, Y[L] = y1;

	for (i = 1; i <= L; i++)
		for (j = 0; j < MAXL; j++) 
		{
			cx = X[i] + dirx[j];
			cy = Y[i] + diry[j];

			if (cx>0 && cy>0 && cx<=N && cy<=M && A[cx][cy] && V[cx][cy]==-1)
			{
				L++;
				X[L] = cx, Y[L] = cy;
				V[cx][cy] = V[X[i]][Y[i]] + 1;
			}
		}

	return V[x2][y2];
}

int main()
{
	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 ", &x1, &y1, &x2, &y2);

	for (i = 1; i <= N; i++)
		for (j = 1; j <= M; j++) scanf("%d ", &A[i][j]);

	printf("%d\n", BFS(x1, y1, x2, y2));

	return 0;
}