grasshop.c
#include <stdio.h>
int width,height,fx,fy;
int check[101][101];
void rek(int sx, int sy, int step)
{
static int stepX[] = {1, 2, 2, 1, -1, -2, -2, -1};
static int stepY[] = {2, 1, -1, -2, -2, -1, 1, 2};
int i, nextX, nextY;
check[sx][sy] = step;
for(i=0; i<8; i++){
nextX = sx + stepX[i];
nextY = sy + stepY[i];
if(nextX < 0 || nextX >= width) continue;
if(nextY < 0 || nextY >= height) continue;
if(check[nextX][nextY] > step+1)
rek(nextX, nextY, step+1);
}
return;
}
int main( int agrc, char * argv[] ) {
int sx,sy,i,j;
while( scanf("%d %d %d %d %d %d", &width
,&height
,&sx
,&sy
,&fx
,&fy
) != EOF
) {
for (i=0; i<101;i++)
for (j=0; j<101;j++)
check[i][j]=200000000;
rek(sx, sy, 0);
if(check[fx][fy] == 200000000)
else
printf("%d\n", check
[fx
][fy
]);
}
return 0;
}