# 数据结构之广度优先搜索（队列实现）问题

1032人阅读 评论(0)

## Description

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

Input

Output

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

(4,4)
(3,4)
(2,4)
(2,3)
(2,2)
(2,1)
(2,0)
(1,0)
(0,0)

## 代码：

#include<stdio.h>

#define MAX_ROW 5
#define MAX_COL 5

int head = 0, tail = 0;

struct Node {
int row;
int col;
int before;
} queue[512];

int maze[MAX_ROW][MAX_COL] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};

void push_queue(struct Node node)
{   /*这个地方不是head++*/
queue[tail++] = node;
}

struct Node pop_queue()
{   /*注意这个地方不是tail++**/
return queue[head++];
}

int is_empty()
{
return tail == head;
}

void visit(int row, int col)
{
struct Node p = { row, col, head - 1 };
/*给访问过的点赋值为3 */
/*注意这个地方不是用 p = {3}; */
maze[row][col] = 3;
push_queue(p);
}

void printf_queue()
{
for (int i = 0; i < MAX_ROW; i++)
{
for (int j = 0; j < MAX_COL; j++)
{
printf("%d\t", maze[i][j]);
}
printf("\n");
}
printf("***********\n");
}

int main()
{
struct Node p = {0, 0, -1};
maze[p.row][p.col] = 3;
push_queue(p);
printf("is empty %d\n", is_empty());
while (!is_empty())
{
p = pop_queue();
if (p.row == MAX_ROW - 1 && p.col == MAX_COL - 1)
{
break;
}
if (p.col + 1 < MAX_COL && maze[p.row][p.col + 1] == 0)
{
visit(p.row, p.col + 1);  /**右边**/
}
if (p.row + 1 < MAX_ROW && maze[p.row + 1][p.col] == 0)
{
visit(p.row + 1, p.col); /**下边**/
}
if (p.col - 1 >= 0 && maze[p.row][p.col - 1] == 0)
{
visit(p.row, p.col - 1); /**左边**/
}
if (p.row - 1 >= 0 && maze[p.row - 1][p.col])
{
visit(p.row - 1, p.col); /**上边**/
}
printf_queue();

}
if (p.row == MAX_ROW - 1 && p.col == MAX_COL - 1)
{
/*记得这里也要打印一行不然后面结果会少一行*/
printf("(%d,%d)\n", p.row, p.col);
while (p.before != -1)
{
p = queue[p.before];
printf("(%d,%d)\n", p.row, p.col);
}
}
else
{
printf("no this path");
}

return 0;
}

## 结果展示：

is empty 0
3	1	0	0	0
3	1	0	1	0
0	0	0	0	0
0	1	1	1	0
0	0	0	1	0
***********
3	1	0	0	0
3	1	0	1	0
3	0	0	0	0
0	1	1	1	0
0	0	0	1	0
***********
3	1	0	0	0
3	1	0	1	0
3	3	0	0	0
3	1	1	1	0
0	0	0	1	0
***********
3	1	0	0	0
3	1	0	1	0
3	3	0	0	0
3	1	1	1	0
0	0	0	1	0
***********
3	1	0	0	0
3	3	0	1	0
3	3	3	0	0
3	1	1	1	0
0	0	0	1	0
***********
3	1	0	0	0
3	3	0	1	0
3	3	3	0	0
3	1	1	1	0
3	0	0	1	0
***********
3	1	0	0	0
3	3	0	1	0
3	3	3	0	0
3	1	1	1	0
3	0	0	1	0
***********
3	1	0	0	0
3	3	0	1	0
3	3	3	3	0
3	1	1	1	0
3	0	0	1	0
***********
3	3	0	0	0
3	3	3	1	0
3	3	3	3	0
3	1	1	1	0
3	0	0	1	0
***********
3	3	0	0	0
3	3	3	1	0
3	3	3	3	0
3	1	1	1	0
3	3	0	1	0
***********
3	3	0	0	0
3	3	3	1	0
3	3	3	3	0
3	1	1	1	0
3	3	0	1	0
***********
3	3	0	0	0
3	3	3	1	0
3	3	3	3	0
3	1	1	1	0
3	3	0	1	0
***********
3	3	0	0	0
3	3	3	3	0
3	3	3	3	3
3	1	1	1	0
3	3	0	1	0
***********
3	3	0	0	0
3	3	3	3	0
3	3	3	3	3
3	1	1	1	0
3	3	0	1	0
***********
3	3	3	0	0
3	3	3	3	0
3	3	3	3	3
3	1	1	1	0
3	3	0	1	0
***********
3	3	3	0	0
3	3	3	3	0
3	3	3	3	3
3	3	1	1	0
3	3	3	1	0
***********
3	3	3	0	0
3	3	3	3	0
3	3	3	3	3
3	3	1	1	0
3	3	3	1	0
***********
3	3	3	0	0
3	3	3	3	0
3	3	3	3	3
3	3	1	1	0
3	3	3	1	0
***********
3	3	3	0	0
3	3	3	3	0
3	3	3	3	3
3	3	1	1	3
3	3	3	1	0
***********
3	3	3	0	0
3	3	3	3	3
3	3	3	3	3
3	3	1	1	3
3	3	3	1	0
***********
3	3	3	3	0
3	3	3	3	3
3	3	3	3	3
3	3	1	1	3
3	3	3	1	0
***********
3	3	3	3	0
3	3	3	3	3
3	3	3	3	3
3	3	3	1	3
3	3	3	1	0
***********
3	3	3	3	0
3	3	3	3	3
3	3	3	3	3
3	3	3	1	3
3	3	3	1	0
***********
3	3	3	3	0
3	3	3	3	3
3	3	3	3	3
3	3	3	1	3
3	3	3	1	0
***********
3	3	3	3	0
3	3	3	3	3
3	3	3	3	3
3	3	3	1	3
3	3	3	1	0
***********
3	3	3	3	0
3	3	3	3	3
3	3	3	3	3
3	3	3	1	3
3	3	3	1	3
***********
3	3	3	3	0
3	3	3	3	3
3	3	3	3	3
3	3	3	1	3
3	3	3	1	3
***********
3	3	3	3	3
3	3	3	3	3
3	3	3	3	3
3	3	3	1	3
3	3	3	1	3
***********
3	3	3	3	3
3	3	3	3	3
3	3	3	3	3
3	3	3	1	3
3	3	3	1	3
***********
3	3	3	3	3
3	3	3	3	3
3	3	3	3	3
3	3	3	1	3
3	3	3	1	3
***********
3	3	3	3	3
3	3	3	3	3
3	3	3	3	3
3	3	3	1	3
3	3	3	1	3
***********
(4,4)
(3,4)
(2,4)
(2,3)
(2,2)
(2,1)
(2,0)
(1,0)
(0,0)


个人资料
等级：
访问量： 120万+
积分： 1万+
排名： 555
联系方式
最新评论