Description [title]
Enter n
Representatives of a n- × n-
Board, the input start position and end position coordinates, asked a Knight of eight directions of the board cursory word step, from start to finish coordinate coordinate how many steps you can go through.
[Enter]
First, enter a n
It indicates the number of test samples.
Each test sample has three rows.
The first line is the size of the board L (4 ≦ L ≤300)
;
The second and third rows represent the horse starting position and the target position (0 .. L -1)
。
[Output]
Horse moves minimum number of steps, the starting position and the target position are the same output 0
。
[Sample input]
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
[Sample Output]
5
28
0
// Created on 2020/2/9
/*#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <climits>*/
#include <bits/stdc++.h>
#define MAXX 40*40+5
using namespace std;
//int i,j,k;
const int maxn=INT_MAX;
const int idata=100000+5;
//int aim[idata];
bool judge[305][305];
bool flag;
int soux,souy,
exitx,exity;
//int maps[idata][idata];
const int change[8][2]={{2,1},{2,-1},{1,2},{1,-2},
{-2,1},{-2,-1},{-1,2},{-1,-2}};
int n,m;
int ans[idata];
int nx,ny;
int x[idata],y[idata];
int main()
{
int head,tail;
int i,j;
int t;
cin>>t;
while(t--)
{
flag=0;
memset(judge,0,sizeof(judge));
cin>>n;
cin>>soux>>souy;
cin>>exitx>>exity;
if(soux==exitx&&souy==exity)
{
cout<<0<<endl;
continue;
}
head=0,tail=1;
x[1]=soux,y[1]=souy;
judge[soux][souy]=1;
ans[1]=0;
do
{
head++;
for(i=0;i<8;i++)
{
nx=x[head]+change[i][0];
ny=y[head]+change[i][1];
if(nx>=0&&ny>=0&&nx<n&&ny<n
&&!judge[nx][ny])
{
judge[nx][ny]=1;
tail++;
x[tail]=nx;
y[tail]=ny;
ans[tail]=ans[head]+1;
}
if(nx==exitx&&ny==exity)
{
flag=1;
cout<<ans[tail]<<endl;
break;
}
}
if(flag) break;
}while(head<tail);
}
return 0;
}