![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b6f41372cbaae65a432ce67be5df4c7b.png)
int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={-1,0,1,-1,1,-1,0,1};
int shortestPathBinaryMatrix(int** grid, int gridSize, int* gridColSize){
if(grid[0][0]!=0||grid[gridSize-1][gridSize-1]!=0){
return -1;
}
grid[0][0]=1;
typedef struct
{
int x;
int y;
}data;
int front, rear;
data* queue = malloc((gridSize*gridColSize[0])*sizeof(data));
memset(queue,0,(gridSize*gridColSize[0])*sizeof(data));
front = rear = 0;
queue[rear].x = 0;
queue[rear].y = 0;
rear++;
while(front<rear){
data tmp=queue[front++];
int length=grid[tmp.x][tmp.y];
for(int a=0;a<8;a++){
int newX=tmp.x+dx[a];
int newY=tmp.y+dy[a];
if(newX>=0&&newX<gridSize&&newY>=0&&newY<gridSize&&grid[newX][newY]==0){
grid[newX][newY]=length+1;
queue[rear].x=newX;
queue[rear].y=newY;
rear++;
}
}
}
if(grid[gridSize-1][gridSize-1]==0){
return -1;
}
return grid[gridSize-1][gridSize-1];
}