#include <iostream>
#include <queue>
using namespace std;
int x1,y1,x2,y2,m,chess[310][310],chess2[310][310];
int knightx[8] = {1,1,-1,-1,2,2,-2,-2};
int knighty[8] = {2,-2,2,-2,1,-1,1,-1};
void bfs()
{
memset(chess,0,sizeof(chess));
memset(chess2,0,sizeof(chess2));
queue <int>p;
p.push(x1);
p.push(y1);
while(!p.empty())
{
int vx,vy;
vx = p.front();p.pop();
vy = p.front();p.pop();
chess[vx][vy] = 1;
if(vx == x2 && vy == y2) break;
for(int i = 0;i < 8 ;i ++)
{
int tx,ty;
tx = vx + knightx[i];
ty = vy + knighty[i];
if(!chess[tx][ty] && tx >= 0 && tx < m && ty >= 0 && ty < m)
{
p.push(tx);
p.push(ty);
chess[tx][ty] = 1;
chess2[tx][ty] = chess2[vx][vy] + 1;
}
}
}
}
int main()
{
int n;
cin >> n;
while(n --)
{
cin >> m;
cin >> x1 >> y1;
cin >> x2 >> y2;
bfs();
cout << chess2[x2][y2] << endl;
}
return 0;
}
poj1915
最新推荐文章于 2023-03-12 15:04:14 发布