leetcode490题,迷宫,求到某个点是否存在路径
leetcode505题,迷宫II,求到某个点的最小路径count
leetcode994题,腐烂的橘子,求各自中没有新鲜橘子经历的最小分钟数。
算法核心就是
1、写好入队函数的判断
2、写好外面函数入队的场景
3、二维数组需要构造结构体包含x,y坐标
迷宫我用maze[x][y]==2标识这个区域是否被访问过
迷宫II我用maze[x][y]来标识到达当前位置所花的时间
迷宫
#define MAXLEN 10000
typedef struct {
int x;
int y;
}Queue;
Queue g_queue[MAXLEN];
int Enqueue(int x, int y, int tail, int **maze)
{
if (maze[x][y] == 2) {
return tail;
}
else {
g_queue[tail].x = x;
g_queue[tail].y = y;
maze[x][y] = 2;
}
return tail + 1;
}
bool hasPath(int** maze, int mazeSize, int* mazeColSize, int* start, int startSize, int* destination, int destinationSize) {
int row, col, head, tail, dx, dy;
if (mazeSize == 0) {
return false;
}
if (start[0] == destination[0] && start[1] == destination[1]) {
return true;
}
row = mazeSize;
col = *mazeColSize;
dx = destination[0];
dy = destination[1];
head = 0;
tail = 0;
g_queue[tail].x = start[0];
g_queue[tail].y = start[1];
maze[g_queue[tail].x][g_queue[tail].y] = 2;
tail++;
int tmpx, tmpy, count;
while (head < tail) {
tmpx = g_queue[head].x;
tmpy = g_queue[head].y;
if (tmpx==dx

本文介绍了使用BFS(广度优先搜索)算法解决LeetCode中的三道题目:490题迷宫问题,505题迷宫II的最短路径计数,以及994题腐烂橘子的最小时间步数。关键在于正确实现入队判断和外部函数的入队情况,以及使用结构体存储二维数组的坐标信息。对于迷宫问题,通过标记已访问区域;对于迷宫II,记录到达每个位置的时间;对于腐烂的橘子,追踪没有新鲜橘子的最小分钟数。
最低0.47元/天 解锁文章
&spm=1001.2101.3001.5002&articleId=105067658&d=1&t=3&u=57b584592faf4611ac65f36fdf65b0bc)
1984

被折叠的 条评论
为什么被折叠?



