8 8
1 1 1 1 1 1 1 1
0 0 0 0 0 1 1 1
1 0 1 1 0 0 0 1
1 0 1 1 0 1 1 0
1 0 1 1 1 1 1 1
1 0 1 1 0 0 0 1
1 0 0 0 0 1 0 0
1 1 1 1 1 1 1 0
2 1
8 8

16

### AC代码：

``````#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)
#define ms(a,x) memset(a,x,sizeof(a))
#define P pair<int,int>
#define mp(x,y) make_pair(x,y)
const int INF = 0x3f3f3f3f;   //无穷大
const int maxn = 505;
int G[maxn][maxn];      //迷宫地图,G[i][j]=1为通,0为不通
bool vis[maxn][maxn];   //判断vis[i][j]是否被访问过
P pre[maxn][maxn];    //pre[i][j]表示走到(i,j)的前一步
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int m,n;
cin >> m >> n;
ms(G,-INF);
ms(vis,false);
Up(i,1,m)
{
Up(j,1,n)
{
cin >> G[i][j];
}
}
P a,b;   //起点坐标a,终点坐标b
cin >> a.first >> a.second >> b.first >> b.second;
queue<P> q;
q.push(a);
while(!q.empty())
{
P cur = q.front();
q.pop();
int x = cur.first, y = cur.second;
vis[x][y] = true;
if(x==m && y==n)
{
break;
}
if(x>1 && !vis[x-1][y] && !G[x-1][y]) //若G[x-1][y]=1且该点未被访问过
{
q.push(mp(x-1,y));
pre[x-1][y] = cur;
}
if(y>1 && !vis[x][y-1] && !G[x][y-1])
{
q.push(mp(x,y-1));
pre[x][y-1] = cur;
}
if(x<m && !vis[x+1][y] && !G[x+1][y])
{
q.push(mp(x+1,y));
pre[x+1][y] = cur;
}
if(y<n && !vis[x][y+1] && !G[x][y+1])
{
q.push(mp(x,y+1));
pre[x][y+1] = cur;
}
}
int ans = 1;   //从a到b所需的最少电路材料
P cur = b;   //从终点开始回溯
while(cur.first!=a.first || cur.second!=a.second)   //直到回溯到起点为止
{
ans++;
cur = pre[cur.first][cur.second];
}
cout << ans << endl;
return 0;
}``````