c语言队列实现迷宫算法

Posted on 2012-05-08 14:06  逝水如年  阅读(3907)  评论(0)    收藏  举报

一、c语言广度搜索算法

1、环形队列实现

 

 1 #include <stdio.h>
 2 #define MAX_ROW 5
 3 #define MAX_COL 5
 4 /*利用队列方式来完成对一个迷宫数组进行深度搜索*/
 5 int head = 0, tail = 0;
 6 /*有一个5行,5列的数组,看成一个迷宫*/
 7 int maze[MAX_ROW][MAX_COL] = {
 8         {0, 1, 0, 0, 0},
 9         {0, 1, 0, 1, 1},
10         {0, 0, 0, 0, 0},
11         {0, 1, 1, 1, 0},
12         {0, 0, 0, 1, 0},
13 };
14 
15 /*队列数组,每个单元都是一个point结构体*/
16 struct point{int row,col;} queue[512];
17 
18 /*入栈*/
19 void enqueue(struct point per)
20 {
21     if(tail == 512){
22         tail = 0;
23     }
24     queue[tail++] = per;
25 }
26 
27 /*出栈*/
28 struct point dequeue(void)
29 {
30     if(head == 512){
31         head = 0;
32     }
33     return queue[head++];
34 }
35 
36 /*判断队列是否为空*/
37 int is_empty(void)
38 {
39     return head == tail;
40 }
41 
42 /*将当前搜索到的位置压入队列*/
43 void visit(int row, int col)
44 {
45     struct point p = {row,col};
46     //将搜索到的数组中的位置为0的地方标记为2一面进行2次搜索
47     maze[row][col] = 2;
48     enqueue(p);
49 }
50 
51 /*打印每一步*/
52 void maze_print()
53 {
54     int i, j;
55     for(i=0; i<MAX_ROW; i++){
56         for(j=0; j<MAX_COL; j++){
57             printf("%d, ",maze[i][j]);
58         }
59         putchar('\n');
60     }
61     printf("**************\n");
62 }
63 
64 int main(void)
65 {
66     //初始化一个位置结构,并将起点设置为2
67     struct point p = {0, 0};
68     maze[p.row][p.col] = 2;
69     //将起点压入栈中
70     enqueue(p);
71     while(!is_empty()){
72         p = dequeue();
73         if(p.row == MAX_ROW - 1 && p.col == MAX_COL - 1)
74             break;                                      /*goal*/
75         if(p.col + 1 < MAX_COL && maze[p.row][p.col+1] == 0)
76             visit(p.row, p.col + 1);                    /*right*/
77         if(p.row + 1 < MAX_ROW && maze[p.row+1][p.col] == 0)
78             visit(p.row + 1, p.col);                    /*down*/
79         if(p.col - 1 >= 0 && maze[p.row][p.col-1] == 0)
80             visit(p.row, p.col - 1);                    /*left*/                                                                                                                
81         if(p.row - 1 >= 0 && maze[p.row-1][p.col] == 0)
82             visit(p.row - 1,p.col);                     /*top*/
83         maze_print();
84     }
85     return 1;
86 }

二、c语言深度搜索算法

 1 #include <stdio.h>
 2 #define MAX_ROW 5
 3 #define MAX_COL 5
 4 /*利用队列方式来完成对一个迷宫数组进行深度搜索*/
 5 int top = 0;
 6 /*有一个5行,5列的数组,看成一个迷宫*/
 7 int maze[MAX_ROW][MAX_COL] = {
 8         {0, 1, 0, 0, 0},
 9         {0, 1, 0, 1, 1},
10         {0, 0, 0, 0, 0},
11         {0, 1, 1, 1, 0},
12         {0, 0, 0, 1, 0},
13 };
14 
15 /*队列数组,每个单元都是一个point结构体*/
16 struct point{int row,col;} stack[512];
17 
18 /*入栈*/
19 void push(struct point per)
20 {
21     stack[top++] = per;
22 }
23 
24 /*出栈*/
25 struct point pop(void)
26 {
27     return stack[--top];
28 }
29 
30 /*判断队列是否为空*/
31 int is_empty(void)
32 {
33     return top == 0;
34 }
35 
36 /*将当前搜索到的位置压入队列*/
37 void visit(int row, int col)
38 {
39     struct point p = {row,col};
40     //将搜索到的数组中的位置为0的地方标记为2一面进行2次搜索
41     maze[row][col] = 2;
42     push(p);
43 }
44 
45 /*打印每一步*/
46 void maze_print()
47 {
48     int i, j;
49     for(i=0; i<MAX_ROW; i++){
50         for(j=0; j<MAX_COL; j++){
51             printf("%d, ",maze[i][j]);
52         }
53         putchar('\n');
54     }
55     printf("**************\n");
56 }
57 
58 int main(void)
59 {
60     //初始化一个位置结构,并将起点设置为2
61     struct point p = {0, 0};
62     maze[p.row][p.col] = 2;
63     //将起点压入栈中
64     push(p);
65     while(!is_empty()){
66         p = pop();
67         if(p.row == MAX_ROW - 1 && p.col == MAX_COL - 1)
68             break;                                      /*goal*/
69         if(p.col + 1 < MAX_COL && maze[p.row][p.col+1] == 0)
70             visit(p.row, p.col + 1);                    /*right*/
71         if(p.row + 1 < MAX_ROW && maze[p.row+1][p.col] == 0)
72             visit(p.row + 1, p.col);                    /*down*/
73         if(p.col - 1 >= 0 && maze[p.row][p.col-1] == 0)
74             visit(p.row, p.col - 1);                    /*left*/                                                                                                                
75         if(p.row - 1 >= 0 && maze[p.row-1][p.col] == 0)
76             visit(p.row - 1,p.col);                     /*top*/
77         maze_print();
78     }
79     return 1;
80 }

 三、递归队列实现广度搜索

 

 1 #include <stdio.h>
 2 #define MAX_ROW 5
 3 #define MAX_COL 5
 4 
 5 /*利用队列方式来完成对一个迷宫数组进行深度搜索*/
 6 int head = 0, tail = 0;
 7 struct pos{int row,col;} queue[512];
 8 struct pos p = {0, 0};
 9 /*有一个5行,5列的数组,看成一个迷宫*/
10 int maze[MAX_ROW][MAX_COL] = {
11         {0, 1, 0, 0, 0},
12         {0, 1, 0, 1, 1},
13         {0, 0, 0, 0, 0},
14         {0, 1, 1, 1, 0},
15         {0, 0, 0, 1, 0},
16 };
17 
18 /*入栈*/
19 void enqueue(struct pos per)
20 {
21     if(tail == 512){
22         tail = 0;
23     }
24     queue[tail++] = per;
25 }
26 
27 /*出栈*/
28 struct pos dequeue(void)
29 {
30     if(head == 512){
31         head = 0;
32     }
33     return queue[head++];
34 }
35 
36 /*打印每一步*/
37 void maze_print()
38 {
39     int i, j;
40     for(i=0; i<MAX_ROW; i++){
41         for(j=0; j<MAX_COL; j++){
42             printf("%d, ",maze[i][j]);
43         }
44         putchar('\n');
45     }
46     printf("**************\n");
47 }
48 
49 /*将当前搜索到的位置压入队列*/
50 void visit(int row, int col)
51 {
52     struct pos pp = {row,col};
53     //将搜索到的数组中的位置为0的地方标记为2一面进行2次搜索
54     maze[row][col] = 2;
55     enqueue(pp);
56 }
57 
58 /*利用递归实现迷宫*/
59 void maze_recursive()
60 {
61     p = dequeue();
62     if(p.row == MAX_ROW - 1 && p.col == MAX_COL - 1)
63         return;                                     /*goal*/
64     if(p.col + 1 < MAX_COL && maze[p.row][p.col+1] == 0)
65         visit(p.row, p.col + 1);                    /*right*/
66     if(p.row + 1 < MAX_ROW && maze[p.row+1][p.col] == 0)
67         visit(p.row + 1, p.col);                    /*down*/
68     if(p.col - 1 >= 0 && maze[p.row][p.col-1] == 0)
69         visit(p.row, p.col - 1);                    /*left*/                                                                                                                
70     if(p.row - 1 >= 0 && maze[p.row-1][p.col] == 0)
71         visit(p.row - 1,p.col);                     /*top*/
72     maze_print();
73     maze_recursive();
74 }
75 
76 int main(void)
77 {
78     maze[p.row][p.col] = 2;
79     enqueue(p);
80     maze_recursive();
81     return 1;
82 }

 

 

 

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3