队列:广度优先搜索求解迷宫

这个算是完全自己写的

Code:
  1. /*广度优先æ?œç´¢æ±‚解迷宫*/  
  2. #include<stdio.h>  
  3.   
  4. #define MAX_ROW 5  
  5. #define MAX_COL 5  
  6. #define LEN 512  
  7.   
  8. typedef struct  
  9. {  
  10.     int row;  
  11.     int col;  
  12.     int pre;/*the front node, saved the subscript of the queue array*/  
  13. }point;  
  14.   
  15. point queue[LEN];  
  16. int tail = 0, head = 0;  
  17. point start = {0, 0, -1};  
  18. point target = {4, 4, -1};  
  19.   
  20. void in_queue(point item)  
  21. {  
  22.     queue[tail++] = item;  
  23. }  
  24.   
  25. point out_queue(void)  
  26. {  
  27.     return queue[head++];  
  28. }  
  29.   
  30. int is_empty(void)  
  31. {  
  32.     return head == tail;  
  33. }  
  34.   
  35. int maze[MAX_ROW][MAX_COL] = {  
  36.     {0, 1, 0, 0, 0},  
  37.     {0, 1, 0, 1, 0},  
  38.     {0, 0, 0, 0, 0},  
  39.     {0, 1, 1, 1, 0},  
  40.     {0, 0, 0, 1, 0}  
  41. };  
  42. /*ä¸?能使用此方法,因为并å?‘çš„æ?œç´¢å?¯èƒ½å¯¼è‡´æŸ?节点的å‰?驱改å?˜*/  
  43. //int front[MAX_ROW][MAX_COL] = {0};  
  44.   
  45. void print_maze(void)  
  46. {  
  47.     int i, j;  
  48.   
  49.     for(i = 0 ; i < MAX_ROW; i++)  
  50.     {  
  51.         for(j = 0; j < MAX_COL; j++)  
  52.         {  
  53.             printf("%d   ", maze[i][j]);  
  54.         }  
  55.   
  56.         putchar('/n');  
  57.     }  
  58. }  
  59.   
  60. void print_queue()  
  61. {  
  62.     int i;  
  63.   
  64.     printf("/nthe queue info:/n");  
  65.     for(i = 0; i < tail; i++)  
  66.     {  
  67.         printf("(%d,%d,%d)  ", queue[i].row, queue[i].col, queue[i].pre);  
  68.     }  
  69.   
  70.     printf("/n##############################/n");  
  71. }  
  72.   
  73. void visit(int row, int col)  
  74. {  
  75.     point tmp = {row, col, head - 1};  
  76.   
  77.     maze[row][col] = 2;  
  78.   
  79.     in_queue(tmp);  
  80. }  
  81.   
  82. void print_way(void)  
  83. {  
  84.     point tmp = queue[tail - 1];  
  85.     while(!(tmp.row == start.row && tmp.col == start.col))  
  86.     {  
  87. //      getchar();  
  88.         printf("(%d,%d)/n", tmp.row, tmp.col);  
  89.         tmp = queue[tmp.pre];  
  90. //      print_queue();  
  91.     }  
  92.   
  93.     printf("(%d,%d)/n", start.row, start.col);  
  94. }  
  95.   
  96. int main(void)  
  97. {  
  98.     point p = start;  
  99.   
  100.     maze[start.row][start.col] = 2;  
  101.     in_queue(p);  
  102.   
  103.     while(!(is_empty()))  
  104.     {  
  105.         p = out_queue();  
  106.         print_maze();  
  107. //      print_queue();  
  108. //      getchar();  
  109.         if((p.row == target.row) && (p.col == target.col))  
  110.         {  
  111.             static int count = 0;  
  112.             printf("%d way:/n", ++count);  
  113.             print_way();  
  114.             break;  
  115. //          back_track(p);  
  116.         }  
  117.   
  118.         if((p.row - 1 >= 0) && (maze[p.row - 1][p.col] == 0))/*found the up way*/  
  119.         {  
  120.             visit(p.row - 1, p.col);  
  121.         }  
  122.   
  123.         if((p.col + 1 < MAX_COL) && (maze[p.row ][p.col + 1] == 0))/*found the left way*/  
  124.         {  
  125.             visit(p.row, p.col + 1);  
  126.         }  
  127.   
  128.         if((p.col - 1 >= 0) && (maze[p.row][p.col - 1] == 0))/*found the right way*/  
  129.         {  
  130.             visit(p.row, p.col - 1);  
  131.         }  
  132.   
  133.         if((p.row + 1 < MAX_ROW) && (maze[p.row + 1][p.col] == 0))/*found the down way*/  
  134.         {  
  135.             visit(p.row + 1, p.col);  
  136.         }  
  137.     }  
  138.           
  139.     if(!((p.row == target.row) && (p.col == target.col)))  
  140.     {  
  141.         printf("Can not found!/n");  
  142.     }  
  143.   
  144.     return 0;  
  145. }  

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值