Cod sursa(job #254602)

Utilizator Andreid91Ciocan Andrei Andreid91 Data 7 februarie 2009 13:11:09
Problema Kdrum Scor 0
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 1.3 kb
#include<fstream.h>


int k,p,u,nr[100],sw,x1,x2,y1,y2,i,j,viz[60][60],v[100][100],n,m,l,c,a[100],b[100];



int  main()
{
ifstream f("kdrum.in");
f>>n>>m>>k>>x1>>y1>>x2>>y2;
for (i=1;i<=n;i++)
	for (j=1;j<=m;j++)
		f>>v[i][j];
viz[1][1]=1;
p=u=1;
a[p]=x1;
b[p]=y1;
nr[p]=0;
sw=0;
while (sw==0)
	{
	l=a[p];
	c=b[p];
	if ((l<n) && (v[l+1][c]!=0) && (viz[l+1][c]==0))
				     {
				     u++;
				     viz[l+1][c]=1;
				     a[u]=l+1;
				     b[u]=c;
				     if ((a[u]==x2) && (b[u]==y2)) sw=u;
				     nr[u]=nr[p]+1;
				     }
	if ((l>1) && (v[l-1][c]!=0) && (viz[l-1][c]==0))
				     {
				     u++;
				     viz[l-1][c]=1;
				     a[u]=l-1;
				     b[u]=c;
				     if ((a[u]==x2) && (b[u]==y2)) sw=u;
				     nr[u]=nr[p]+1;
				     }

	if ((c<m) && (v[l][c+1]!=0) && (viz[l][c+1]==0))
				     {
				     u++;
				     viz[l][c+1]=1;
				     a[u]=l;
				     b[u]=c+1;
				     if ((a[u]==x2) && (b[u]==y2)) sw=u;
				     nr[u]=nr[p]+1;
				     }

	if ((c>1) && (v[l][c-1]!=0) && (viz[l][c-1]==0))
				     {
				     u++;
				     viz[l][c-1]=1;
				     a[u]=l;
				     b[u]=c-1;
				     if ((a[u]==x2) && (b[u]==y2)) sw=u;
				     nr[u]=nr[p]+1;
				     }
	p++;
	}
f.close();
ofstream g("kdrum.out");
g<<nr[sw];
g.close();
return 0;
}