BFS迷宫搜索路径

  1 #include<graphics.h>
  2 #include<stdlib.h>
  3 #include<conio.h>
  4 #include<time.h>
  5 #include<vector>
  6 #include<queue> 
  7 #include<stack> 
  8 #include<iostream>
  9 #include <algorithm>
 10 
 11 using namespace std;
 12 
 13 struct point{ 
 14     //横坐标纵坐标 
 15     int x; 
 16     int y; 
 17 }; 
 18 int **Maze;     //初始化迷宫 
 19 point **Pre;    //保存任意点在路径中的前一步 
 20 point move[8]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; //移动方向,横竖斜都可以,八个方向 
 21 char s1[]="YES";
 22 char s2[]="NO";
 23 
 24 void create_line(int row ,int  column)//划线
 25 {
 26   int x, y;  // 画格子
 27    for (x=0; x<=10*(row+2); x+=10)
 28         for (y=0; y<=10*(column+2); y+=10)
 29         {
 30             line(x, 0, x, 10*(column+2));
 31             line(0, y,10*(row+2), y);
 32         }
 33 }
 34 
 35 
 36 void CreateTable(int row,int column)//初始化创建迷宫障碍区
 37 {
 38     int i,j;
 39     srand((unsigned int)time(NULL));
 40     for(i=0; i<row+2; i++)//创建迷宫,注意到用0表示可走,1表示墙,将整个输入的迷宫再用墙围着,处理的时候就不用特别注意边界问题 
 41     {
 42         Maze[i][0] = Maze[i][column+1] = 1; 
 43     }
 44     for(j=0; j<column+2; j++) 
 45     {
 46         Maze[0][j] = Maze[row+1][j] = 1;       
 47     }
 48 
 49     for(i=1;i<=row;i++)
 50      for(j=1;j<=column;j++)
 51         {
 52              Maze[i][j]=rand()%2;
 53         }
 54 
 55    for(i=1;i<=row;i++)
 56      for(j=1;j<=column;j++)
 57         {
 58              if(Maze[i][j]==1)
 59             Maze[i][j]=rand()%2;
 60         }
 61 
 62     for(i=0;i<=row+1;i++)
 63      for(j=0;j<=column+1;j++)
 64      {
 65          if(Maze[i][j]==1)
 66         {
 67          setfillcolor(WHITE);
 68          fillrectangle(10*j,(10+10*i),(10+10*j),10*i);
 69         }
 70      }
 71 }
 72 
 73 bool MazePath(int row,int column,int x,int y){ 
 74     //判断是否有路径从入口到出口,保存该路径(队列) 
 75     if(x == row && y == column)return true; 
 76     queue<point> q;     //用于广度优先搜索 
 77     point now;          //当前位置 
 78     now.x = x; 
 79     now.y = y; 
 80     q.push(now); 
 81     Maze[now.x][now.y] = -1; 
 82     while(!q.empty()){ 
 83         now = q.front(); 
 84         q.pop(); 
 85         for(int i=0; i<8; i++){ 
 86             if(now.x + move[i].x == row && now.y + move[i].y == column){ 
 87                 Maze[now.x + move[i].x][now.y + move[i].y] = -1; 
 88                 Pre[row][column] = now; 
 89                 return true; 
 90             } 
 91             if(Maze[now.x + move[i].x][now.y + move[i].y] == 0){ 
 92                 point temp;     //下个位置 
 93                 temp.x = now.x + move[i].x; 
 94                 temp.y = now.y + move[i].y; 
 95                 q.push(temp); 
 96                 Maze[temp.x][temp.y] = -1; 
 97                 Pre[temp.x][temp.y] = now; 
 98 
 99             } 
100         } 
101     } 
102     return false; 
103 } 
104 
105 
106 void PrintPath(int row,int column){ 
107     //输出最短路径 
108     point temp;         //保存位置 
109     stack<point> s;     //保存路径序列 
110     temp.x = row; 
111     temp.y = column; 
112     while(temp.x != 1 || temp.y != 1){ 
113         s.push(temp); 
114         temp = Pre[temp.x][temp.y]; 
115     } 
116        setfillcolor(YELLOW);
117        fillrectangle(10*1,(10+10*1),(10+10*1),10*1);
118     while(!s.empty()){ 
119         temp = s.top(); 
120         setfillcolor(YELLOW);
121         fillrectangle(10*temp.y,(10+10*temp.x),(10+10*temp.y),10*temp.x);
122         s.pop(); 
123     } 
124     cout<<endl; 
125 }
126 
127 void result(int row,int column)
128 {
129   if(MazePath(row,column,1,1))
130   { 
131             //outtextxy(320,320,s1);
132             PrintPath(row,column); 
133   } 
134         else outtextxy(320,320,s2);
135 }
136 
137 void Init(int row,int column)
138 {
139  Maze = new int*[row + 2]; 
140  Pre = new point*[row + 2]; 
141  for(int i=0; i<row+2; i++)
142  { 
143  Maze[i] = new int[column + 2]; 
144  Pre[i] = new point[column + 2]; 
145  }
146 }
147 
148 void main()
149 {
150 int row,column;
151 cin>>row>>column;
152 Init(row,column);
153 initgraph(640,640);
154 BeginBatchDraw();
155 setlinecolor(GREEN);//设置字体颜色
156 create_line(column,row);
157 CreateTable(row,column);
158 result(row,column);
159 FlushBatchDraw();
160 EndBatchDraw();
161 getch();
162 closegraph();
163 }

原文地址:https://www.cnblogs.com/wxdjss/p/5588780.html