ololo.cpp
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <memory.h>
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define FI(i,b) FOR(i,0,b)
#define V(t) vector < t >
#define pb push_back
#define MEMS(a,b) memset((a),(b),sizeof(a))
using namespace std;
int dx[]={1,1,-1,-1,2,2,-2,-2};
int dy[]={2,-2,2,-2,1,-1,1,-1};
int n,m;
int was[110][110];
int p[110*110][2];
int d[110][110];
inline bool iscor(int x, int y)
{
return ((x>=0) && (y>=0) && (x<=n-1) && (y<=m-1));
}
int main()
{
int xs,ys,xt,yt;
while (scanf("%d%d%d%d%d%d",&n,&m,&xs,&ys,&xt,&yt)!=EOF)
{
MEMS(was,0);
//MEMS(p,0);
//MEMS(d,0);
int l=0,r=0;
was[xs][ys]=1;
d[xs][ys]=0;
p[0][0]=xs;
p[0][1]=ys;
int res=-1;
while (l<=r)
{
int x=p[l][0];
int y=p[l][1];
if ((x==xt) && (y==yt))
{
res=d[x][y];
break;
}
FOR(k,0,8)
{
int nx=x+dx[k];
int ny=y+dy[k];
if (iscor(nx,ny) && (was[nx][ny]==0))
{
was[nx][ny]=1;
d[nx][ny]=d[x][y]+1;
r++;
p[r][0]=nx;
p[r][1]=ny;
}
}
l++;
}
if (res>=0)
printf("%d\n",res);
else
printf("impossible\n");
}
return 0;
}