走迷宫_BFS

昨天刚接触BFS算法,用的queue队列,算法的理解应该没什么问题,但是BFS和DFS在输出最短的路径上有点不一样,BFS如果要输出路径就必须要做记录,然后在逆向DFS,我用的是逆向链表记录路径,队列弹出推入的都是结构体地址。

  1 /*
  2   Name: 老鼠走迷宫(二) BFS
  3   Copyright: 
  4   Author: 顾骏 
  5   Date: 23/05/13 22:14
  6   Description: BFS
  7 */
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <queue>
 11 #include <windows.h>
 12 using namespace std;
 13 
 14 typedef struct node
 15 {
 16    int x;
 17    int y;
 18    int move;
 19    struct node* last;
 20 }point;
 21 point start={1,1,0,NULL},end={7,7};
 22 
 23 void visit(void);
 24 int maze[9][9]={
 25                   {2,2,2,2,2,2,2,2,2},
 26                   {2,0,0,0,0,0,0,0,2},
 27                   {2,0,2,2,0,2,2,0,2},
 28                   {2,0,2,0,0,2,0,0,2},
 29                   {2,0,2,0,2,0,2,0,2},
 30                   {2,0,0,0,0,0,2,0,2},
 31                   {2,2,0,2,2,0,2,2,2},
 32                   {2,0,0,0,0,0,0,0,2},
 33                   {2,2,2,2,2,2,2,2,2}
 34                },
 35    dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
 36 
 37 int main()
 38 {
 39    int i,j;
 40    printf("Show:\n");
 41    for(i=0;i<9;i++)
 42    {
 43       for(j=0;j<9;j++)
 44          {
 45             if(maze[i][j]==2)
 46                printf(""); 
 47             else
 48                printf("  ");
 49          }
 50       printf("\n");
 51    }
 52    visit();
 53    system("pause");
 54    return 0;
 55 }
 56 
 57 void visit(void)
 58 {
 59    queue<point*> p,p_free;
 60    p.push(&start);
 61    point* temp;
 62    while(!p.empty())
 63       {
 64          temp=p.front();
 65          p.pop();
 66          for(int i=0;i<4;i++)
 67             {
 68                point* next=(point*)malloc(sizeof(point));
 69                p_free.push(next);
 70                next->last=temp;
 71                next->x=temp->x+dir[i][0];
 72                next->y=temp->y+dir[i][1];
 73                
 74                if(maze[next->x][next->y]==0)
 75                   {
 76                      next->move=temp->move+1;
 77                      maze[next->x][next->y]=1;
 78                      
 79                      if(next->x==end.x&&next->y==end.y)
 80                         {  
 81                            //回溯出路径 
 82                            for(point* q=next;q!=NULL;q=q->last)
 83                               {
 84                                  maze[q->x][q->y]=3;
 85                               }
 86                            //输出路径 
 87                            printf("\nmove:%d\n",next->move);
 88                            for(int j=0;j<9;j++)
 89                               {
 90                                  for(int k=0;k<9;k++)
 91                                     {
 92                                        if(maze[j][k]==2)
 93                                           printf("");
 94                                        else if(maze[j][k]==3)
 95                                           printf("");
 96                                        else
 97                                           printf("  ");
 98                                     }
 99                                  printf("\n");
100                               }
101                            //释放内存
102                            while(!p_free.empty())
103                               {
104                                  temp=p_free.front();
105                                  free(temp);
106                                  p_free.pop();
107                               }
108                            return;
109                         }
110                      p.push(next);
111                   }
112             } 
113       }
114 }

 

posted @ 2013-06-06 12:17  瓶哥  Views(509)  Comments(0Edit  收藏  举报