主要求最短路
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[110][110];
bool check[110][110];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int w[110][110];
int n,m;
typedef pair<int, int> PII;
PII c[110][110];
int bfs()
{
queue<PII>q;
q.push({1,1});
check[1][1]=1;
while(!q.empty())
{
PII p=q.front();
q.pop();
for(int i=0;i<4;i++)
{ int x=p.first+dx[i];
int y=p.second+dy[i];
if(a[x][y]==0 && !check[x][y]&&x<=n&&y<=m&&x>=1&&y>=1)
{ c[x][y]=p;
check[x][y]=1;
w[x][y]=w[p.first][p.second]+1;
q.push({x,y});
}
}
}
// int x=n,y=m;//返回路径
// while(x||y)
//{
// cout<<x<<" "<<y<<endl;
//auto t=c[x][y];
// x=t.first;y=t.second;
//}
cout<<w[n][m];
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
bfs();
}