题目链接:活动 - AcWing
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 100+5;
int map[maxn][maxn],dp[maxn][maxn];
typedef pair<int,int> Pint;
int main()
{
int n,m;
int dx[] = {0,0,-1,1},dy[] ={1,-1,0,0};
scanf("%d%d",&n,&m);
memset(map,1,sizeof(map));
memset(dp,0,sizeof(dp));
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
scanf("%d",&map[i][j]);
queue<Pint>path;
path.push({1,1});
dp[1][1] = 0;
while(!path.empty())
{
Pint dot = path.front();
path.pop();
for(int i = 0;i < 4;i++)
{
int dx_next = dot.first+dx[i],dy_next = dot.second+dy[i];
if(map[dx_next][dy_next] == 0 && dp[dx_next][dy_next] == 0)
{
dp[dx_next][dy_next] = dp[dot.first][dot.second]+1;
path.push({dx_next,dy_next});
}
}
}
printf("%d",dp[n][m]);
return 0;
}