Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226242
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: C/C++

2017-02-21 12:47:48

本程序通过栈的机制(先进后出)来走迷宫。

原理如下:

int buff[11][11] = {
    {1,1,1,1,1,1,1,1,1,1,1},
    {1,0,1,1,0,1,0,1,1,0,0},
    {1,0,1,1,0,1,0,1,0,0,0},
    {1,0,0,1,0,0,0,0,0,1,0},
    {1,0,1,1,0,1,0,1,1,0,0},
    {1,0,1,1,0,1,0,0,0,0,1},
    {1,0,1,0,0,1,0,1,1,0,1},
    {1,0,0,0,0,0,0,0,1,0,0},
    {1,0,1,1,0,0,0,0,0,1,0},
    {1,0,1,1,0,1,0,1,0,1,0},
    {1,0,1,1,0,1,0,0,0,0,0}
};

其中 1 为石头,0为通路。以 x=1,y=1为起点.x=10,y=10为终点。
现在先模拟走迷宫

#define GG  程序搜索栈顶的点的上、下、左、右、四个方向是否可以通过,其中某一个方向可以通过。停止搜索其他方向

1. 将(1,1)压入栈中,然后 GG  。找到(2,1)为0 可以走。
2. 将(2,2) 压栈,此时栈中的内容为top ->(2,2)(1,1) ->buttom.此时再次GG  。(3,1)可以走
以此类推..

直到找到终点为止
但是本次使用的是深度优先搜索算法,得到的并不是最佳的路径,仅仅是一条可靠的路径而已。

本次走迷宫使用深度优先搜索算法的目的是找出所有的走迷宫的方法、路径。这个功能后续优化

点击(此处)折叠或打开

  1. #include <stdio.h>

  2. #define SIZE 10

  3. typedef struct point{
  4.     int x;
  5.     int y;
  6.     int Step;
  7. }Point;

  8. Point Stack[100];
  9. int buttom;
  10. int top;

  11. int visit[SIZE+1][SIZE+1];
  12. int Show_buff[11][11];

  13. int buff[SIZE+1][SIZE+1] = {
  14.     {1,1,1,1,1,1,1,1,1,1,1},
  15.     {1,0,1,1,0,1,0,1,1,0,0},
  16.     {1,0,1,1,0,1,0,1,0,0,0},
  17.     {1,0,0,0,0,0,0,0,0,1,0},
  18.     {1,0,1,1,0,1,0,1,1,0,0},
  19.     {1,0,1,1,0,1,0,0,0,0,1},
  20.     {1,0,1,0,0,1,0,1,1,0,1},
  21.     {1,0,0,0,0,0,0,0,1,0,0},
  22.     {1,0,1,1,0,0,0,0,0,1,0},
  23.     {1,0,1,1,0,1,0,1,0,1,0},
  24.     {1,0,1,1,0,1,0,0,0,0,0}
  25. };

  26. void Init_Stack(void)
  27. {
  28.     int i;
  29.     for(i=0;i<100;i++){
  30.         Stack[i].x = 0;
  31.         Stack[i].y = 0;
  32.     }
  33.     buttom = 0;
  34.     top = 0;
  35. }

  36. int Stack_Is_Empty()
  37. {
  38.     if(buttom == top)
  39.         return 1;

  40.     return 0;
  41. }

  42. void Add_Stack_Top(Point p)
  43. {
  44.     Stack[top] = p;
  45.     
  46.     top++;
  47.     if(top>99){
  48.         top = 0;
  49.     }
  50. }

  51. Point Del_Stack_Top(void)
  52. {
  53.     Point p;

  54.     top--;
  55.     if(top<0){
  56.         top = 99;
  57.     }

  58.     p = Stack[top];
  59.     Stack[top].x = 0;
  60.     Stack[top].y = 0;
  61.     
  62.     return p;
  63. }

  64. Point Get_Stack_Top(void)
  65. {
  66.     return Stack[top-1];
  67. }

  68. void Init_Visit(void)
  69. {
  70.     int i,j;
  71.     for(i=0;i<11;i++){
  72.         for(j=0;j<1;j++){
  73.             visit[i][j] = 0;
  74.         }
  75.     }
  76. }

  77. int Is_Visit(int x, int y){

  78.     if((visit[x][y] == 1) || (visit[x][y] == 2))
  79.         return 1;

  80.     return 0;
  81. }

  82. void Set_Visit(Point p)
  83. {
  84.     visit[p.x][p.y] = 1;
  85. }
  86. void Set_Visit_Die(Point p)
  87. {
  88.     visit[p.x][p.y] = 2;
  89. }


  90. int direction[4][2] = {
  91.     {-1,0},{1,0},{0,-1},{0,1} //up down left right
  92. };

  93. void Disply_Stack(void)
  94. {
  95.     Point p;
  96.     int w_head,w_tail;
  97.     w_head = buttom;
  98.     w_tail = top;

  99.     printf("\n---------------------------------------------\n");
  100.     while(w_head != w_tail){

  101.         p = Stack[w_head];
  102.         w_head ++;
  103.             
  104.         printf("( %d,%d )",p.x,p.y);
  105.         if(w_head>99)
  106.             w_head=0;
  107.         
  108.     }
  109.     printf("\n---------------------------------------------\n");
  110. }

  111. void Disply_Game(void)
  112. {
  113.     Point p;
  114.     int i,j,w_head,w_tail;
  115.     w_head = buttom;
  116.     w_tail = top;

  117.     for(i=0;i<11;i++){
  118.         for(j=0;j<11;j++)
  119.         {
  120.             Show_buff[i][j] = 0;
  121.         }
  122.     }
  123.     
  124.     while(w_head != w_tail){

  125.         p = Stack[w_head];
  126.         w_head ++;
  127.         Show_buff[p.x][p.y] = 1;
  128.         if(w_head>99)
  129.             w_head=0;
  130.         
  131.     }
  132.     printf("\n------------------------------------------------\n");
  133.     for(i=1;i<11;i++){
  134.         printf(" ");
  135.         for(j=1;j<11;j++)
  136.         {
  137.             if(Show_buff[i][j] == 1){
  138.                 printf("0 ");
  139.             }else{
  140.                 printf(" ");
  141.             }
  142.         }
  143.         printf(" \n");
  144.     }
  145.     printf("------------------------------------------------\n");
  146. }

  147. int DFS(Point start, Point end)
  148. {
  149.     int i,count=0;
  150.     Point temp,Add_temp;
  151.     Set_Visit(start);
  152.     Add_Stack_Top(start);

  153.     if(buff[end.x][end.y] == 1){
  154.         printf("end (%d,%d) is 1 can't goto",end.x, end.y);
  155.         return 0;
  156.     }

  157.     while(!Stack_Is_Empty()){
  158.         Disply_Stack();
  159.         temp = Get_Stack_Top();

  160.         for(i=0;i<4;i++){
  161.             
  162.             if((temp.x+direction[i][0] > 10) || (temp.x+ direction[i][0] <1) || (temp.y + direction[i][1] > 10) || (temp.y + direction[i][1] < 1)){
  163.                 continue;
  164.             }

  165.             if((temp.x+direction[i][0] == end.x) && (temp.y+direction[i][1] == end.y)){

  166.                 printf("\nI find it (%d,%d) Step = %d \n",end.x, end.y, temp.Step);
  167.                 
  168.                 return 1;
  169.             }

  170.             if((!Is_Visit(temp.x+direction[i][0],temp.y +direction[i][1])) && (buff[temp.x +direction[i][0]][temp.y + direction[i][1]] == 0)){
  171.                 Add_temp.x = temp.x+direction[i][0];
  172.                 Add_temp.y = temp.y+direction[i][1];
  173.                 Add_temp.Step = temp.Step + 1;
  174.                 Add_Stack_Top(Add_temp);
  175.                 Set_Visit(Add_temp);
  176.                 break;
  177.             }
  178.             if(i==3){
  179.                 temp = Del_Stack_Top();
  180.                 printf("delete post = %d %d",temp.x,temp.y);
  181.                 Set_Visit_Die(temp);
  182.             }
  183.         }
  184.     }

  185.      printf("\n end n");
  186.     return 0;
  187. }

  188. int main(void){

  189.     Point p_start,p_end;
  190.     p_start.x = 1;
  191.     p_start.y = 1;
  192.     p_start.Step = 1;

  193.     p_end.x = 10;
  194.     p_end.y = 10;

  195.     Init_Visit();
  196.     Init_Stack();
  197.     
  198.     if(DFS(p_start, p_end))
  199.         Disply_Game();

  200.     return 0;
  201. }




阅读(1572) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~