栈:深度优先搜索与回溯算法求解迷宫

利用栈和回溯算法求解迷宫:

这是老师带着做的,自己写不知道什么时候能调出来~~

这就是和老师的差距,这样的程序他就10几分钟而已~

Code:
  1. /*深度优先æ?œç´¢æ±‚解迷宫*/  
  2. #include<stdio.h>  
  3. #define MAX_ROW 5  
  4. #define MAX_COL 5  
  5.   
  6. int maze[MAX_ROW][MAX_COL] = {  
  7.     { 0, 1, 0, 0, 0 },  
  8.     { 0, 1, 0, 1, 0 },  
  9.     { 0, 0, 0, 0, 0 },  
  10.     { 0, 1, 1, 1, 0 },  
  11.     { 0, 0, 0, 1, 0 },  
  12. };  
  13.   
  14. struct point{  
  15.     int row;  
  16.     int col;  
  17. }stack[512];  
  18.   
  19. struct point front[MAX_ROW][MAX_COL]={  
  20.     {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},  
  21.     {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},  
  22.     {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},  
  23.     {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},  
  24.     {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},  
  25. };  
  26.   
  27. int top = 0;  
  28. struct point start = {0, 0};  
  29. struct point target = {4, 4};  
  30. struct point full;  
  31.   
  32. void push(struct point p)  
  33. {  
  34.     stack[top++] = p;  
  35. }  
  36.   
  37.   
  38. struct point pop(void)  
  39. {  
  40.     return stack[--top];  
  41. }  
  42.   
  43.   
  44. int is_empty(void)  
  45. {  
  46.     return top == 0;  
  47. }  
  48.   
  49. struct point get_top(void)  
  50. {  
  51.     return stack[top-1];  
  52. }  
  53.   
  54. void print_stack(void)  
  55. {  
  56.     int i;  
  57.   
  58.     printf("stack info: /n");  
  59.     for(i = top -1; i >= 0; i--)  
  60.     {  
  61.         printf("(%d,%d) %d/n",stack[i].row, stack[i].col,  
  62.                stack[i].row * MAX_COL + stack[i].col);  
  63.     }  
  64. }  
  65.   
  66.   
  67. void print_maze(void)  
  68. {  
  69.     int i,j;  
  70.     for(i = 0; i < MAX_ROW; i++)  
  71.     {  
  72.         for(j = 0; j < MAX_COL; j++)  
  73.             printf("%d   ",maze[i][j]);  
  74.         putchar('/n');  
  75.     }  
  76.   
  77.     printf("*****************/n");  
  78. }  
  79.   
  80. void print_pre()  
  81. {  
  82.     int i, j;  
  83.     int row, col;  
  84.   
  85.     for(i = 0; i < MAX_ROW; i++)  
  86.     {  
  87.         for(j = 0; j < MAX_COL; j++)  
  88.         {  
  89.             row = front[i][j].row;  
  90.             col = front[i][j].col;  
  91.             printf("%d(%d)/t", row*MAX_COL + col, i*MAX_COL + j);  
  92.         }  
  93.         printf("/n");  
  94.     }  
  95.   
  96. }  
  97.   
  98. void print_front(void)  
  99. {  
  100.     struct point tmp = target;  
  101.     static int  ways = 0;  
  102.   
  103.     printf("%d solution is:/n", ++ways);  
  104.     while(!(tmp.row == start.row && tmp.col == start.col))  
  105.     {  
  106.         printf("(%d,%d)/n", tmp.row, tmp.col);  
  107.         tmp = front[tmp.row][tmp.col];  
  108.     }  
  109.     printf("(%d,%d)/n", start.row, start.col);  
  110. }  
  111.   
  112. void visit(int newrow, int newcol, struct point p)  
  113. {  
  114.     struct point tmp;  
  115.   
  116.     maze[newrow][newcol] = 2;  
  117.     /*set new expend node 's father node*/  
  118.     front[newrow][newcol] = p;  
  119.       
  120.     tmp.row = newrow;  
  121.     tmp.col = newcol;  
  122.     push(tmp);  
  123.       
  124. }  
  125.   
  126. void back_track(struct point p)  
  127. {  
  128.     struct point back = get_top();  
  129.   
  130.     back = front[back.row][back.col];  
  131.   
  132.     while(!(back.row == p.row && back.col == p.col))  
  133.     {  
  134.         maze[p.row][p.col] = 0;  
  135.         p = front[p.row][p.col];  
  136.         print_stack();  
  137.         print_maze();  
  138.         getchar();  
  139.     }  
  140. }  
  141. /*void back_track_full(struct point p) 
  142. { 
  143.     full = front[full.row][full.col]; 
  144.     back_track(p,full); 
  145. }*/  
  146.   
  147. int main(viod)  
  148. {  
  149.     struct point p, node;  
  150.     int flag = 1;  
  151.   
  152.     printf( "hello,maze! /n" );  
  153.     print_maze();  
  154.   
  155.     /*init start situation*/  
  156.     p = start;  
  157.     maze[p.row][p.col] = 2;  
  158.     push(p);  
  159. //  print_stack();  
  160.   
  161.     while( !is_empty() )  
  162.     {  
  163.         /*get the saved point from stack*/  
  164.         p = pop();  
  165.         /*judge id p is target point*/  
  166.   
  167.         if((p.row == target.row) && (p.col == target.col))  
  168.         {         
  169.             printf("target is found!/n");  
  170.             print_front();  
  171.             back_track(p);  
  172.             continue;  
  173.         }  
  174.           
  175.         flag = 0;  
  176.         /*expend p to UP. LEFT,DOWN, RIGHT*/  
  177.         if( ((p.row - 1) >= 0) && (maze[p.row - 1][p.col] == 0) )/*UP*/  
  178.         {  
  179.             visit( p.row - 1, p.col, p );  
  180.             flag++ ;  
  181.         }  
  182.   
  183.         if( ((p.col - 1) >= 0) && (maze[p.row][p.col - 1] == 0) )/*LEFT*/  
  184.         {  
  185.             visit( p.row, p.col - 1, p );  
  186.             flag++;  
  187.         }  
  188.   
  189.         if( ((p.row + 1) < MAX_ROW) && (maze[p.row + 1][p.col] == 0) )/*DOWN*/  
  190.         {  
  191.             visit( p.row + 1, p.col, p );  
  192.             flag++;  
  193.         }  
  194.           
  195.         if( ((p.col + 1) < MAX_COL) && (maze[p.row][p.col + 1] == 0) )/*RIGHT*/  
  196.         {  
  197.             visit( p.row, p.col + 1, p );  
  198.             flag++;  
  199.         }  
  200.         if(flag == 0)  
  201.             back_track(p);    
  202.         print_stack();  
  203.         print_pre();  
  204.           
  205. //      getchar();  
  206.     }  
  207.           
  208.     return 0;  
  209. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值