Cod sursa(job #254457)

Utilizator AndreyPAndrei Poenaru AndreyP Data 7 februarie 2009 12:16:16
Problema Kdrum Scor 20
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 0.98 kb
#include<stdio.h>
#define N 55
int a[N][N];
int n,m,k;
int x1,y1,x2,y2;
struct coada
{
	int c,cost;
};
const int dx[]={-1,0,1,0};
const int dy[]={0,1,0,-1};
inline int mp(int x,int y)
{
	return (x<<8)+y;
}
inline void citire()
{
	scanf("%d%d%d",&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("%d",&a[i][j]);
	}
}
void bfsn()
{
	coada c[300];
	int p=0,u=0;
	c[0].c=mp(x1,y1);
	c[0].cost=1;
	int xnow,ynow,xnext,ynext;
	while(p<=u)
	{
		xnow=c[p].c>>8;
		ynow=c[p].c&255;
		int cost=c[p].cost+1;
		++p;
		for(int i=0; i<4; ++i)
		{
			xnext=xnow+dx[i];
			ynext=ynow+dy[i];
			if(!a[xnext][ynext])
				continue;
			if(xnext==x2 && ynext==y2)
			{
				printf("%d\n",cost);
				return;
			}
			a[xnext][ynext]=0;
			c[++u].c=mp(xnext,ynext);
			c[u].cost=cost;
		}
	}
}
int main()
{
	freopen("kdrum.in","r",stdin);
	freopen("kdrum.out","w",stdout);
	citire();
	bfsn();
	return 0;
}