您的位置:首页 > 其它

用循环队列解迷宫问题

2013-12-30 09:26 106 查看
/***********************************************************************************

队列是一种先进先出的机制,用队列来解迷宫问题的思想如下:

将起点标记为已走过并入队列;

while (队列非空)

{

出队一个点p;

if (p这个点是终点)

{

break;

}

否则沿下,右,上,左四个方向探索相邻的点

if (和p相邻的点有路可走,并且还没走过)

{

将相邻的点标记为已走过并入队,它的前趋就是刚出队的p点;

}

}

if (p点是终点)

{

while (p点有前趋)

{

打印p点的坐标;

p点 = p点的前趋;

}

}

else

{

没有路线可以到达终点;

}

***************************************************************************************/

#include <stdio.h>

#define MAX_ROW 8

#define MAX_COL 8

#define MAX_LEN (MAX_ROW * MAX_COL)

#define false 0

#define true 1

typedef struct point

{

int row;

int col;

}Point;

Point queue[MAX_LEN];

unsigned int head = -1;

unsigned int tail = -1;

unsigned int flag = 0;

int maze[MAX_ROW][MAX_COL] = {

0, 1, 0, 1, 0, 0, 1, 0,

0, 0, 1, 0, 1, 0, 0, 1,

0, 0, 0, 0, 1, 1, 1, 1,

1, 0, 1, 0, 1, 0, 0, 1,

1, 0, 1, 0, 0, 1, 1, 1,

1, 1, 1, 0, 0, 0, 0, 1,

0, 0, 0, 0, 0, 1, 1, 1,

1, 1, 1, 1, 0, 0, 0, 0,

};

Point FootPrints[MAX_ROW][MAX_COL] = {

{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},

{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},

{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},

{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},

{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},

{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},

{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},

{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},

};

void Print_Maze(void)

{

int i = 0, j = 0;

for(i = 0; i < MAX_ROW; i++)

{

for(j = 0; j < MAX_COL; j++)

{

printf("%2d", maze[i][j]);

}

printf("\n");

}

printf("*************************************\n");

}

int QueueEmpty(void)

{

if(tail == head)

{

head = -1;/*如果是空对列,则置头尾都为-1,重新开始出入队列*/

tail = -1;

return true;

}

return false;

}

int QueueFull(void)

{

if((tail+1 == head) || ((head == 0) && (MAX_LEN-1 == tail)))

{

return true;/*队列满的时候有两种情况:1,可以理解为未出对列,直接入满;2,在中间位置入队列反超导致满*/

}

else

{

return false;

}

}

int InQueue(Point p)

{

if(QueueFull())/*队列满的时候*/

{

return false;

}

if(tail == -1)/*队列为空的标识,则置head,tail都为0,又从队列首地址开始出入队列操作*/

{

head = 0;

tail = 0;

}

else if(MAX_LEN-1 == tail)

{

tail = 0;

}

queue[tail++] = p;/*入队列*/

return true;

}

int OutQueue(Point *pPoint)

{

if(QueueEmpty())

{

return false;

}

if(head == MAX_LEN-1)

{

head = 0;

}

*pPoint = queue[head++];

return true;

}

void Print_Foot(void)

{

Point p = {MAX_ROW-1, MAX_COL-1};

while(FootPrints[p.row][p.col].row != -1)

{

printf("{%d, %d} \n", p.row, p.col);

p = FootPrints[p.row][p.col];

}

printf(" seccess out ! \n");

}

void VisitPoint(int row, int col, Point p)

{

Point tempPoint = {row, col};

maze[row][col] = 2;/*访问的点要标记为2*/

InQueue(tempPoint);/*访问的点要入队列*/

FootPrints[row][col] = p;/*当前点的坐标记录上一个点的坐标*/

}

int main(void)

{

Point p;

Point first = {0, 0};/*设定出发点*/

maze[first.row][first.col] = 2;/*访问过的点标记为2*/

InQueue(first);/*访问的点要入队列*/

while(! QueueEmpty())

{

Print_Maze();/*打印迷宫图*/

OutQueue(& p);/*出队列*/

if((p.row+1 == MAX_ROW) && (p.col+1 == MAX_COL))

{

printf("it's go out ! \n");

flag = 1;

break;

}

if((p.row+1 < MAX_ROW) && (maze[p.row+1][p.col] == 0))

{

VisitPoint(p.row+1, p.col, p);

}

if((p.col+1 < MAX_COL) && (maze[p.row][p.col+1] == 0))

{

VisitPoint(p.row, p.col+1, p);

}

if((p.row-1 >= 0) && (maze[p.row-1][p.col] == 0))

{

VisitPoint(p.row-1, p.col, p);

}

if((p.col-1 >= 0) && (maze[p.row][p.col-1] == 0))

{

VisitPoint(p.row, p.col-1, p);

}

}

if(flag)

{

Print_Foot();

}

else

{

printf("No Path ! \n");

}

return 0;

}

/*******************************************************************

从打印的结果可以看出,这个算法的特点是沿着每个点的各个方向同时

展开搜索,每个可以走通的方向轮流往前走一步,这称为广度优先搜索

(BFS,Breadth First Search)。广度优先搜索是一种步步为营的策略,

每次都从各个方向探索一步,将前线推进一步,队列中的元素总是由各个

方向上可以走得通的点组成的,可见正是队列先进先出的性质使这个算法

具有了广度优先的特点。广度优先搜索还有一个特点是可以找到从起点到

终点的最短路径,而深度优先搜索找到的不一定是最短路径。

********************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: