Cod sursa(job #254332)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 7 februarie 2009 11:19:03
Problema Kdrum Scor 20
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 0.87 kb
#include<stdio.h>
struct queue1
  {
  int x,y;
  long dist;
  };

int n,m,k;
int x1,y1,x2,y2;
int x[]={0,-1,0,1,0};
int y[]={0,0,1,0,-1};
long v[53][53];


void read()
{
freopen("kdrum.in","r",stdin);
freopen("kdrum.out","w",stdout);
scanf("%d%d%d",&n,&m,&k);
int i,j;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
for(i=1;i<=n;i++)
  for(j=1;j<=m;j++)
    scanf("%ld",&v[i][j]);
}


long lee1()
{
int i,u,p,xt,yt;
queue1 q[3000];
u=p=1;
q[1].x=x1;
q[1].y=y1;
q[1].dist=1;
while(p<=u)
  {
  for(i=1;i<=4;i++)
    {
    xt=q[p].x+x[i];
    yt=q[p].y+y[i];
    if(v[xt][yt])
      {
      u++;
      q[u].x=xt;
      q[u].y=yt;
      q[u].dist=q[p].dist+1;
      if(xt==x2 && yt==y2)
	return q[u].dist;
      v[xt][yt]=0;
      }
    }
  p++;
  }
}


int main()
{
read();
if(k==1)
  printf("%ld",lee1());
else
  printf("%d",n+m-1);
return 0;
}