C算法-BFS(广度优先搜索法)

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 && tmpy ==dy) {
    
    
            return true;
        }
		while (tmpx - 1 >= 0 && maze[tmpx - 1][tmpy] != 1) {
    
    
			tmpx--;
		}
		tail = Enqueue(tmpx, tmpy, tail,maze);
		tmpx = g_queue[head].x;
		while (tmpx + 1 < row && maze[tmpx + 1][tmpy] != 1) {
    
    
			tmpx++;
		}
		tail = Enqueue(tmpx, tmpy, tail,maze);
		tmpx = g_queue[head].x;
		while (tmpy - 1 >= 0 && maze[tmpx][tmpy - 1] != 1) {
    
    
			tmpy--;
		}
		tail = Enqueue(tmpx, tmpy, tail,maze);
		tmpy = g_queue[head].y;
		while (tmpy + 1 < col && maze[tmpx][tmpy + 1] != 1) {
    
    
			tmpy++;
		}
		tail = Enqueue(tmpx, tmpy,tail,maze);
		head++;

	}

	return false;
}


迷宫II

#define MAXLEN 10000
typedef struct {
    
    
	int x;
	int y;
}Queue;

Queue g_queue[MAXLEN];

int Enqueue(int x, int y, int count, int tail, int **maze)
{
    
    
	//printf("x=%d y=%d count=%d premaze=%d \n", x, y, count, maze[x][y]);
	if (maze[x][y] > 0 && maze[x][y] <= count) {
    
    
		return tail;
	}
	else {
    
    
		g_queue[tail].x = x;
		g_queue[tail].y = y;
		maze[x][y] = count;
	}
	//printf("x=%d y=%d count=%d premaze=%d \n", x, y, count, maze[x][y]);
	return tail + 1;
}

int shortestDistance(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 -1;
	}
	if (start[0] == destination[0] && start[1] == destination[1]) {
    
    
		return 0;
	}
	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;
		count = maze[tmpx][tmpy];
		while (tmpx - 1 >= 0 && maze[tmpx - 1][tmpy] != 1) {
    
    
			tmpx--;
			count++;
		}
		tail = Enqueue(tmpx, tmpy, count, tail, maze);
		tmpx = g_queue[head].x;
		count = maze[tmpx][tmpy];
		while (tmpx + 1 < row && maze[tmpx + 1][tmpy] != 1) {
    
    
			tmpx++;
			count++;
		}
		tail = Enqueue(tmpx, tmpy, count, tail, maze);
		tmpx = g_queue[head].x;
		count = maze[tmpx][tmpy];
		while (tmpy - 1 >= 0 && maze[tmpx][tmpy - 1] != 1) {
    
    
			tmpy--;
			count++;
		}
		tail = Enqueue(tmpx, tmpy, count, tail, maze);
		tmpy = g_queue[head].y;
		count = maze[tmpx][tmpy];
		while (tmpy + 1 < col && maze[tmpx][tmpy + 1] != 1) {
    
    
			tmpy++;
			count++;
		}
		tail = Enqueue(tmpx, tmpy, count, tail, maze);
		head++;

	}

	if (maze[dx][dy] == 0) {
    
    
		return -1;
	}
	return maze[dx][dy] - 2;
}

腐烂的橘子

#define MAXLEN 102

typedef struct{
    
    
	int x;
	int y;
}Queue;

Queue q[MAXLEN];

int Enqueue(int i, int j, int row, int col, int ** grid, int tail) 
{
    
    
	if (i < 0 || j < 0 || i >= row || j >= col) {
    
    
		return tail;
	}
	if (grid[i][j] != 1) {
    
    
		return tail;
	}
	grid[i][j] = 2;
	q[tail].x = i;
	q[tail].y = j;
	return tail + 1;
}

int orangesRotting(int** grid, int gridSize, int* gridColSize) {
    
    
	int i, j, tmpi, tmpj, row, col;
	int head, tail;
	head = 0;
	tail = 0;
	row = gridSize;
	col = *gridColSize;
	
	for (i = 0; i < row; i++) {
    
    
		for (j = 0; j < col; j++) {
    
    
			if (grid[i][j] == 2) {
    
    
				q[tail].x = i;
				q[tail].y = j;
				tail++;
			}
		}
	}

	int number, min;
	min = 0;
	while (head < tail) {
    
    
		number = tail - head;
		for (i = 0; i < number; i++) {
    
    
			tmpi = q[head].x;
			tmpj = q[head].y;
			head++;
			tail = Enqueue(tmpi + 1, tmpj, row, col, grid, tail);
			tail = Enqueue(tmpi - 1, tmpj, row, col, grid, tail);
			tail = Enqueue(tmpi, tmpj + 1, row, col, grid, tail);
			tail = Enqueue(tmpi, tmpj - 1, row, col, grid, tail);
		}
        if (head < tail){
    
    
            min++;
        }
	}
	for (i = 0; i < row; i++) {
    
    
		for (j = 0; j < col; j++) {
    
    
			if (grid[i][j] == 1) {
    
    
				min = -1;
			}
		}
	}

	return min;

}

猜你喜欢

转载自blog.csdn.net/weixin_45554139/article/details/105067658