迷宫算法初步

  1 #include<iostream>
  2 #include<stack>
  3 #include<vector>
  4 #include<algorithm>
  5 #define N 6
  6 using namespace std;
  7 int MAP[N][N] = 
  8 {{0,0,0,0,1,0},
  9  {0,0,1,1,0,1},
 10  {0,0,0,1,0,0},
 11  {0,1,0,0,1,0},
 12  {1,1,1,0,1,0},
 13  {0,0,0,0,0,1}
 14 };
 15 struct opint
 16 {
 17     int x;
 18     int y;
 19 
 20 };
 21 vector<opint> note;
 22 bool find_repeat(const opint a)
 23 {
 24     for (auto var : note)
 25     {
 26             if (var.x == a.x&&var.y == a.y)
 27             {
 28                 return 1;
 29             }
 30     }
 31     return 0;
 32 }
 33 struct direction
 34 {
 35     bool up;
 36     bool down;
 37     bool right;
 38     bool left;
 39     int OK;
 40 };
 41 class go
 42 {
 43 public:
 44     opint pint;
 45     direction sign; // 0~3 右 左 上 下
 46     go();
 47     go(int x, int y);
 48     ~go();
 49 
 50 };
 51 go::~go()
 52 {
 53 }
 54 go::go()
 55 {
 56     //创建一个结点。
 57     sign.down=0; 
 58     sign.right = 0;
 59     sign.up = 0;
 60     sign.left = 0;
 61     sign.OK = 0;
 62 
 63 }
 64 go::go(int x, int y)
 65 {
 66     sign.down = 0; //默认是0 代表右
 67     sign.right = 0;
 68     sign.up = 0;
 69     sign.left = 0;
 70     sign.OK = 0;
 71     pint.x = x;
 72     pint.y = y;
 73     note.push_back(pint);
 74 }
 75 
 76 stack<go> road;
 77 
 78 void add(go* a)
 79 {
 80     road.push(*a);
 81 }
 82 void del() //删除结点
 83 {
 84     road.pop();
 85 
 86 }
 87 bool T_move(go* a)
 88 {
 89     opint temp_1;
 90     //进行判断
 91     if (a->pint.x==0)
 92     {
 93         a->sign.up = 1;
 94     }
 95     if (a->pint.x == N - 1)
 96     {
 97         a->sign.down = 1;
 98     }
 99     if (a->pint.y==N-1)
100     {
101         a->sign.right = 1;
102     }
103     if (a->pint.y == 0)
104     {
105         a->sign.left = 1;
106     }
107     a->sign.OK = a->sign.down + a->sign.up + a->sign.right + a->sign.left;
108     if (a->sign.OK>=4)
109     {
110         
111         del();
112         return 1;
113     }
114     //移动
115     if (a->sign.right == 0)
116     {
117         if (a->pint.y < N - 1)
118         {
119             if (MAP[a->pint.x][a->pint.y+1] == 0)
120             {
121                 temp_1.x = a->pint.x;
122                 temp_1.y = a->pint.y + 1;
123                 if (find_repeat(temp_1))
124                 {
125                     a->sign.right = 1;
126                     T_move(a);
127                     return 1;
128                 }
129                 else
130                 {
131                     go* temp = new go(a->pint.x, a->pint.y + 1);
132                     temp->sign.left = 1;
133                     a->sign.right = 1;
134                     add(temp);
135                     return 1;
136                 }
137                 
138             }
139             else
140             {
141                 a->sign.right = 1;
142                 T_move(a);
143                 return 1;
144             }
145         }
146         else
147         {
148             return 0;
149         }
150     }
151     if (a->sign.up == 0)
152     {
153         if (a->pint.x > 0)
154         {
155             if (MAP[a->pint.x-1][a->pint.y] == 0)
156             {
157                 temp_1.x = a->pint.x-1;
158                 temp_1.y = a->pint.y;
159                 if (find_repeat(temp_1))
160                 {
161                     a->sign.up = 1;
162                     T_move(a);
163                     return 1;
164                 }
165                 else
166                 {
167                     go* temp = new go(a->pint.x - 1, a->pint.y);
168                     temp->sign.down = 1;
169                     a->sign.up = 1;
170                     add(temp);
171                     return 1;
172                 }
173                 
174             }
175             else
176             {
177                 a->sign.up = 1;
178                 T_move(a);
179                 return 1;
180             }
181         }
182         return 0;
183     }
184     if (a->sign.left == 0)
185     {
186         if (a->pint.y > 0)
187         {
188 
189 
190             if (MAP[a->pint.x][a->pint.y-1] == 0)
191             {
192                 temp_1.x = a->pint.x;
193                 temp_1.y = a->pint.y - 1;
194                 if (find_repeat(temp_1))
195                 {
196                     a->sign.left = 1;
197                     T_move(a);
198                     return 1;
199                 }
200                 else
201                 {
202                     go* temp = new go(a->pint.x, a->pint.y - 1);
203                     temp->sign.right = 1;
204                     a->sign.left = 1;
205                     add(temp);
206                     return 1;
207                 }
208 
209 
210                 
211             }
212             else
213             {
214                 a->sign.left = 1;
215                 T_move(a);
216                 return 1;
217             }
218         }
219         else
220         {
221             return 0;
222         }
223     }
224 
225     if (a->sign.down == 0)
226     {
227         if (a->pint.x < N-1)
228         {
229 
230 
231             if (MAP[a->pint.x+1][a->pint.y] == 0)
232             {
233                 temp_1.x = a->pint.x + 1;
234                 temp_1.y = a->pint.y;
235                 if (find_repeat(temp_1))
236                 {
237                     a->sign.down = 1;
238                     T_move(a);
239                     return 1;
240                 }
241                 else
242                 {
243                     go* temp = new go(a->pint.x + 1, a->pint.y);
244                     temp->sign.up = 1;
245                     a->sign.down = 1;
246                     add(temp);
247                     return 1;
248                 }
249             }
250             else
251             {
252                 a->sign.down = 1;
253                 T_move(a);
254                 return 1;
255             }
256         }
257         return 0;
258     }
259 }
260 stack<opint> roadline;
261 bool gogo()
262 {
263     while (road.size()>0)
264     {
265         if (road.top().pint.x == 0 && road.top().pint.y == N - 1)
266         {
267             return 1;
268         }
269         T_move(&road.top());
270         
271 
272     }
273     if (road.size()==0)
274     {
275         return 0;//SUCCESS;
276     }
277 }
278 int main()
279 {
280     
281     go a(N-1, 0);
282     road.push(a);
283     if (!gogo())
284     {
285         cout << "走不通" << endl;
286     }
287     else
288     {
289         while (!road.empty())
290         {
291             opint temp;
292             temp.x = road.top().pint.x;
293             temp.y = road.top().pint.y;
294             roadline.push(temp);
295             road.pop();
296         }
297         cout << "" << roadline.size() << "" << "路线如下:" << endl;
298         while (!roadline.empty())
299         {
300             cout << "(" << roadline.top().x << "," << roadline.top().y << ")";
301             roadline.pop();
302             if (roadline.size() >= 1)
303             {
304                 cout << "->";
305             }
306         }
307         cout << endl;
308     }
309     
310     system("pause");
311     return 0;
312 
313 }

还有很大的优化空间,可能还存在Bug待发现

posted on 2017-03-21 16:32  王猛ALL  阅读(120)  评论(0)    收藏  举报

导航