# 最小步数

<span style="color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun; font-size: 14px; background-color: rgb(255, 255, 255);">时间限制：</span><span class="editable highlight" id="problem[time_limit]" style="font-family: Tahoma, Arial, sans-serif, simsun; font-size: 14px; background-color: rgb(255, 255, 255);">3000</span><span style="color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun; font-size: 14px; background-color: rgb(255, 255, 255);"> ms  |  内存限制：</span><span class="editable highlight" id="problem[memory_limit]" style="font-family: Tahoma, Arial, sans-serif, simsun; font-size: 14px; background-color: rgb(255, 255, 255);">65535</span><span style="color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun; font-size: 14px; background-color: rgb(255, 255, 255);"> KB</span>

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

0表示道路，1表示墙。

（注：一步是指从一坐标点走到其上下左右相邻坐标点，如：从（3，1）到（4,1）。）

2
3 1  5 7
3 1  6 7

12
11


[苗栋栋]原创

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

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 x1,y1,x2,y2,k;
int vis[8][8];
int a[4]={-1,1,0,0};
int b[4]={0,0,-1,1};

struct node{
int x,y;
int step;
};
queue<node> Q;

int bfs(int x1,int y1,int x2,int y2)
{
int i,s,t;
node e={x1,y1,0};
Q.push(e);
vis[x1][y1]=1;
while(!Q.empty())
{
e=Q.front();
if(e.x==x2&&e.y==y2)
{
break;
}
Q.pop();
for(i=0;i<4;i++)
{
s=e.x+a[i];
t=e.y+b[i];
if(s>=0&&s<=8&&t>=0&&t<=8&&!vis[s][t]&&map[s][t]==0)
{
node e1={s,t,e.step+1};
Q.push(e1);
vis[s][t]=1;
}
}
}
if(Q.empty())
{
return -1;
}
while(!Q.empty())
{
Q.pop();
}
return e.step;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>x1>>y1>>x2>>y2;
memset(vis,0,sizeof(vis));
k=bfs(x1,y1,x2,y2);
cout<<k<<endl;
}
return 0;
}