1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int vis[305][305]; 6 int trun[8][2]={{1,2},{2,1},{1,-2},{-2,1},{-1,2},{2,-1},{-1,-2},{-2,-1}}; 7 struct point{int x,y;point(int x=0,int y=0):x(x),y(y){}}; 8 int bfs(point s,point t,int l){ 9 memset(vis,0,sizeof(vis)); 10 queue<int>q;q.push(s.x*1000+s.y); 11 while(!q.empty()){ 12 point u=point(q.front()/1000,q.front()%1000);q.pop(); 13 if(u.x==t.x&&u.y==t.y)return vis[t.x][t.y]; 14 for(int i=0;i<8;i++){ 15 point v=point(u.x+trun[i][0],u.y+trun[i][1]); 16 if(v.x>=0&&v.x<l&&v.y>=0&&v.y<l&&!vis[v.x][v.y]) 17 vis[v.x][v.y]=vis[u.x][u.y]+1,q.push(v.x*1000+v.y); 18 } 19 } 20 } 21 int main(){ 22 int n,l,a,b,c,d; 23 for(scanf("%d",&n);n--;) 24 scanf("%d%d%d%d%d",&l,&a,&b,&c,&d),printf("%d ",bfs(point(a,b),point(c,d),l)); 25 return 0; 26 }