#include<stdio.h>
#include<queue>
using namespace std;
int BFS(int a,int b,int c,int d)
{
queue<int> q;
int map[9][9]={{1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,1,0,1},
{1,0,0,1,1,0,0,0,1},
{1,0,1,0,1,1,0,1,1},
{1,0,0,0,0,1,0,0,1},
{1,1,0,1,0,1,0,0,1},
{1,1,0,1,0,1,0,0,1},
{1,1,0,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1}};
int visit[9][9]={0},dis[9][9];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
q.push(a*9+b);
visit[a][b]=1;
dis[a][b]=0;
while(!q.empty())
{
a=q.front()/9;
b=q.front()%9;
q.pop();
for(int i=0;i<4;i++)
{
int x=a+dx[i];
int y=b+dy[i];
if(x>0&&x<9&&y>=0&&y<9&&!map[x][y]&&!visit[x][y])
{
q.push(x*9+y);
visit[x][y]=1;
dis[x][y]=dis[a][b]+1;
}
}
}
return dis[c][d];
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
printf("%d\n",BFS(a,b,c,d));
}
return 0;
}
nyist-58
最新推荐文章于 2023-08-25 09:37:52 发布