grasshop.c
#include <stdio.h>
int Pole[101][101];
int FrontaX[100*100*20];
int FrontaY[100*100*20];
int Zac,Kon;
int R,C,Gr,Gc,Lr,Lc,Cc,Cr,T;
int Found;
void Skoc(int X, int Y, int Time) {
if ((X<1) || (X>C) || (Y<1) || (Y>R)) return;
if ((Pole[X][Y] > Time) || (Pole[X][Y] == -1)) {
Pole[X][Y]=Time;
FrontaX[++Kon]=X;
FrontaY[Kon]=Y;
};
};
int main() {
int x,y;
while (scanf("%d%d%d%d%d%d",&R
,&C
,&Gr
,&Gc
,&Lr
,&Lc
)==6) { for (x=0;x<=R;x++) for (y=0;y<=C;y++) Pole[x][y]=-1;
if ((Gr==Lr) && (Gc == Lc)) {
} else {
FrontaX[0]=Gr;
FrontaY[0]=Gc;
Zac=0;
Kon=0;
Found=0;
Pole[Gr][Gc]=0;
while ((Zac<=Kon) && (Found==0)) {
Cc = FrontaX[Zac];
Cr = FrontaY[Zac++];
T = Pole[Cc][Cr];
//printf("%d %d %d %d %d\n",Cc,Cr,T,Zac,Kon);
Skoc(Cr+2,Cc+1,T+1);
Skoc(Cr+2,Cc-1,T+1);
Skoc(Cr-2,Cc+1,T+1);
Skoc(Cr-2,Cc-1,T+1);
Skoc(Cr+1,Cc+2,T+1);
Skoc(Cr+1,Cc-2,T+1);
Skoc(Cr-1,Cc+2,T+1);
Skoc(Cr-1,Cc-2,T+1);
};
if (Pole[Lr][Lc] == -1) {
} else {
};
};
};
return 0;
};