码迷,mamicode.com
首页 > 编程语言 > 详细

有趣的A*算法演示

时间:2015-05-24 00:01:32      阅读:497      评论:0      收藏:0      [点我收藏+]

标签:

 

技术分享

 

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace A星算法
 12 {
 13     public partial class Form1 : Form
 14     {
 15         int x_count = 20;
 16         int y_count = 20;
 17         //List<AStarMapType> MapData;
 18         AStarMap Map;
 19         //List<Position> Path;
 20         Position start_pos, end_pos;
 21 
 22         int x_last_pos, y_last_pos;
 23         int mode;
 24         public Form1()
 25         {
 26             InitializeComponent();
 27             Map = new AStarMap(x_count, y_count);
 28 
 29 
 30         }
 31 
 32 
 33 
 34         public void redraw()
 35         {
 36             Graphics g = pictureBox1.CreateGraphics();
 37             g.DrawRectangle(Pens.Black, 0, 0, pictureBox1.ClientSize.Width - 1, pictureBox1.ClientSize.Height - 1);
 38             double x_step = (double)(pictureBox1.ClientSize.Width) / x_count;
 39             double y_step = (double)(pictureBox1.ClientSize.Height) / y_count;
 40 
 41             for (int x = 0; x < x_count; x++)
 42             {
 43                 for (int y = 0; y < y_count; y++)
 44                 {
 45                     //g.DrawRectangle(Pens.Black, (float)(x * x_step), (float)(y * y_step), (float)x_step, (float)y_step);
 46                     drawBox(x, y);
 47 
 48                 }
 49             }
 50 
 51 
 52         }
 53 
 54         //public void clearBox(int x, int y)
 55         //{
 56         //    Graphics g = pictureBox1.CreateGraphics();
 57         //    double x_step = (double)(pictureBox1.ClientSize.Width) / x_count;
 58         //    double y_step = (double)(pictureBox1.ClientSize.Height) / y_count;
 59         //    g.FillRectangle(Brushes.White, (float)(x * x_step), (float)(y * y_step), (float)x_step, (float)y_step);
 60         //}
 61 
 62         public void drawBox(int x, int y)
 63         {
 64             Graphics g = pictureBox1.CreateGraphics();
 65             Position pos = new Position(x, y);
 66 
 67             double x_step = (double)(pictureBox1.ClientSize.Width) / x_count;
 68             double y_step = (double)(pictureBox1.ClientSize.Height) / y_count;
 69             g.FillRectangle(Brushes.White, (float)(x * x_step), (float)(y * y_step), (float)x_step, (float)y_step);
 70             g.DrawRectangle(Pens.Black, (float)(x * x_step), (float)(y * y_step), (float)x_step, (float)y_step);
 71             if (Map.GetMapType(pos) == 0)
 72             {
 73                 //   g.DrawRectangle(Pens.Black, (float)(x * x_step), (float)(y * y_step), (float)x_step, (float)y_step);
 74             }
 75             if (Map.GetMapType(pos) == 1)
 76                 g.FillRectangle(Brushes.Brown, (float)(x * x_step) + 1, (float)(y * y_step) + 1, (float)x_step - 2, (float)y_step - 2);
 77 
 78 
 79             if (start_pos != null)
 80                 if (pos == start_pos)
 81                 {
 82                     g.FillEllipse(Brushes.OrangeRed, (float)(x * x_step) + 4, (float)(y * y_step) + 4, (float)x_step - 8, (float)y_step - 8);
 83                 }
 84             if (end_pos != null)
 85                 if (pos == end_pos)
 86                 {
 87                     g.FillEllipse(Brushes.DarkGreen, (float)(x * x_step) + 4, (float)(y * y_step) + 4, (float)x_step - 8, (float)y_step - 8);
 88                 }
 89             //if (Path != null)
 90             //{
 91             //    var f = from m in Path where (m.X == x && m.Y == y) select m;
 92             //    if (f.Count() > 0)
 93             //    {
 94             //        g.FillEllipse(Brushes.OrangeRed, (float)(x * x_step) + 4, (float)(y * y_step) + 4, (float)x_step - 8, (float)y_step - 8);
 95             //    }
 96             //}
 97         }
 98 
 99         private void button1_Click(object sender, EventArgs e)
100         {
101             mode = 1;
102         }
103 
104 
105         private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
106         {
107             double x_step = (double)(pictureBox1.ClientSize.Width) / x_count;
108             double y_step = (double)(pictureBox1.ClientSize.Height) / y_count;
109             int x_pos = (int)(e.X / x_step);
110             int y_pos = (int)(e.Y / y_step);
111             if (x_last_pos != x_pos || y_last_pos != y_pos)
112             {
113                 x_last_pos = x_pos;
114                 y_last_pos = y_pos;
115 
116             }
117         }
118 
119         private void Form1_Load(object sender, EventArgs e)
120         {
121             pictureBox1.Refresh();
122         }
123 
124         private void pictureBox1_Paint(object sender, PaintEventArgs e)
125         {
126             redraw();
127         }
128 
129         private void button2_Click(object sender, EventArgs e)
130         {
131             mode = 2;
132         }
133 
134 
135         //public void clear_start()
136         //{
137         //    for (int y = 0; y < y_count; y++)
138         //    {
139         //        for (int x = 0; x < x_count; x++)
140         //        {
141         //            if (Map.GetMapType(x, y) == 3)
142         //            {
143         //                Map.SetMapType(x, y, 0);
144         //                drawBox(x, y);
145         //            }
146         //        }
147         //    }
148         //}
149 
150         //public void clear_end()
151         //{
152         //    for (int y = 0; y < y_count; y++)
153         //    {
154         //        for (int x = 0; x < x_count; x++)
155         //        {
156         //            if (Map.GetMapType(x, y) == 4)
157         //            {
158         //                Map.SetMapType(x, y, 0);
159         //                drawBox(x, y);
160         //            }
161         //        }
162         //    }
163         //}
164 
165 
166 
167         private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
168         {
169             double x_step = (double)(pictureBox1.ClientSize.Width) / x_count;
170             double y_step = (double)(pictureBox1.ClientSize.Height) / y_count;
171             int x_pos = (int)(e.X / x_step);
172             int y_pos = (int)(e.Y / y_step);
173 
174             Position pos = new Position(x_pos, y_pos);
175 
176             if (mode == 1)
177             {
178                 Map.SetMapType(pos, 1);
179                 drawBox(x_pos, y_pos);
180             }
181             if (mode == 2)
182             {
183                 Map.SetMapType(pos, 0);
184                 drawBox(x_pos, y_pos);
185             }
186             if (mode == 3 && Map.GetMapType(pos) == 0)
187             {
188                 if (start_pos != null)
189                 {
190                     int x = start_pos.X;
191                     int y = start_pos.Y;
192                     start_pos = null;
193                     drawBox(x, y);
194                 }
195                 start_pos = pos;
196                 drawBox(x_pos, y_pos);
197             }
198             if (mode == 4 && Map.GetMapType(pos) == 0)
199             {
200                 if (end_pos != null)
201                 {
202                     int x = end_pos.X;
203                     int y = end_pos.Y;
204                     end_pos = null;
205                     drawBox(x, y);
206                 }
207                 end_pos = pos;
208                 drawBox(x_pos, y_pos);
209             }
210         }
211 
212         private void button3_Click(object sender, EventArgs e)
213         {
214             Map.MapData.Clear();
215             for (int y = 0; y < y_count; y++)
216             {
217                 for (int x = 0; x < x_count; x++)
218                 {
219                     Map.MapData.Add(new AStarMapData(x, y));
220                 }
221             }
222             redraw();
223             mode = 0;
224         }
225 
226         private void button4_Click(object sender, EventArgs e)
227         {
228             mode = 3;
229 
230         }
231 
232         private void button5_Click(object sender, EventArgs e)
233         {
234             mode = 4;
235         }
236 
237         public void drawPath(List<Position> path)
238         {
239             Graphics g = pictureBox1.CreateGraphics();
240             double x_step = (double)(pictureBox1.ClientSize.Width) / x_count;
241             double y_step = (double)(pictureBox1.ClientSize.Height) / y_count;
242             path.Remove(path.First());
243             path.Remove(path.Last());
244             foreach (Position p in path)
245             {
246                 int x = p.X;
247                 int y = p.Y;
248                 g.FillEllipse(Brushes.LightSeaGreen, (float)(x * x_step) + 4, (float)(y * y_step) + 4, (float)x_step - 8, (float)y_step - 8);
249             }
250 
251         }
252 
253         private void button6_Click(object sender, EventArgs e)
254         {
255             if (start_pos != null && end_pos != null)
256             {
257                 AStarSearch s = new AStarSearch(Map, start_pos, end_pos);
258                 if (s.search_path() == 0)
259                 {
260                     drawPath(s.Result);
261                 }
262             }
263         }
264     }
265 }

 

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Diagnostics;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace A星算法
  9 {
 10     class AStarSearch
 11     {
 12         //AStarMapType startNode, endNode;
 13         List<AStarMapData> OpenList;
 14         List<AStarMapData> CloseList;
 15         AStarMap map;
 16         Position StartPos, EndPos;
 17         public List<Position> Result;
 18 
 19         public AStarSearch(AStarMap m, Position start_pos, Position end_pos)
 20         {
 21             map = m;
 22             StartPos = start_pos;
 23             EndPos = end_pos;
 24             OpenList = new List<AStarMapData>();
 25             CloseList = new List<AStarMapData>();
 26             Result = new List<Position>();
 27         }
 28 
 29 
 30         public bool CheckMap(Position pos)
 31         {
 32             if (pos.X < 0 || pos.Y < 0 || pos.X >= map.Width || pos.Y >= map.Height)
 33                 return false;
 34             int r = map.GetMapType(pos);
 35             if (r == 0 || r == 4)
 36                 return true;
 37             else
 38                 return false;
 39         }
 40 
 41         public bool CheckInOpenList(Position pos)
 42         {
 43             var f = from x in OpenList where x.Pos == pos select x;
 44             if (f.Count() > 0)
 45                 return true;
 46             else
 47                 return false;
 48         }
 49 
 50 
 51         public bool CheckInCloseList(Position pos)
 52         {
 53             var f = from x in CloseList where x.Pos == pos select x;
 54             if (f.Count() > 0)
 55                 return true;
 56             else
 57                 return false;
 58         }
 59 
 60 
 61         public int Cal_H(Position a, Position b)
 62         {
 63             return (Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y)) * 10;
 64         }
 65 
 66         public int search_path()
 67         {
 68             int res = -1;
 69 
 70             OpenList.Add(map.GetMapData(StartPos));
 71 
 72             while (true)
 73             {
 74                 if (OpenList.Count == 0)
 75                 {
 76                     res = 1;
 77                     break;
 78                 }
 79                 else
 80                 {
 81                     var f = from x in OpenList orderby x.F select x;
 82                     AStarMapData curMapData = f.First();
 83                     Position pos = curMapData.Pos;
 84                     if (pos == EndPos)
 85                     {
 86                         res = 0;
 87                         break;
 88                     }
 89                     Debug.WriteLine(pos.ToString());
 90                     CloseList.Add(curMapData);
 91                     OpenList.Remove(f.First());
 92 
 93                     #region 左上方
 94                     {
 95                         Position pos1 = new Position(pos.X - 1, pos.Y - 1);
 96                         if (CheckMap(pos1))
 97                         {
 98                             if (CheckInCloseList(pos1) == false)
 99                             {
100                                 AStarMapData md = map.GetMapData(pos1);
101                                 if (CheckInOpenList(pos1) == true)
102                                 {
103                                     if (md.F > curMapData.F + 14)
104                                     {
105                                         md.G = curMapData.F + 14;
106                                         md.H = Cal_H(pos1, EndPos);
107                                         md.F = md.G + md.H;
108                                         md.LastPos = curMapData.Pos;
109                                     }
110                                 }
111                                 else
112                                 {
113                                     md.G = curMapData.F + 14;
114                                     md.H = Cal_H(pos1, EndPos);
115                                     md.F = md.G + md.H;
116                                     md.LastPos = curMapData.Pos;
117                                     OpenList.Add(md);
118                                 }
119                             }
120                         }
121                     }
122                     #endregion
123 
124                     #region 上方
125                     {
126                         Position pos1 = new Position(pos.X, pos.Y - 1);
127                         if (CheckMap(pos1))
128                         {
129                             if (CheckInCloseList(pos1) == false)
130                             {
131                                 AStarMapData md = map.GetMapData(pos1);
132                                 if (CheckInOpenList(pos1) == true)
133                                 {
134                                     if (md.F > curMapData.F + 10)
135                                     {
136                                         md.G = curMapData.F + 10;
137                                         md.H = Cal_H(pos1, EndPos);
138                                         md.F = md.G + md.H;
139                                         md.LastPos = curMapData.Pos;
140                                     }
141                                 }
142                                 else
143                                 {
144                                     md.G = curMapData.F + 10;
145                                     md.H = Cal_H(pos1, EndPos);
146                                     md.F = md.G + md.H;
147                                     md.LastPos = curMapData.Pos;
148                                     OpenList.Add(md);
149                                 }
150                             }
151                         }
152                     }
153                     #endregion
154 
155                     #region 右上方
156                     {
157                         Position pos1 = new Position(pos.X + 1, pos.Y - 1);
158                         if (CheckMap(pos1))
159                         {
160                             if (CheckInCloseList(pos1) == false)
161                             {
162                                 AStarMapData md = map.GetMapData(pos1);
163                                 if (CheckInOpenList(pos1) == true)
164                                 {
165                                     if (md.F > curMapData.F + 14)
166                                     {
167                                         md.G = curMapData.F + 14;
168                                         md.H = Cal_H(pos1, EndPos);
169                                         md.F = md.G + md.H;
170                                         md.LastPos = curMapData.Pos;
171                                     }
172                                 }
173                                 else
174                                 {
175                                     md.G = curMapData.F + 14;
176                                     md.H = Cal_H(pos1, EndPos);
177                                     md.F = md.G + md.H;
178                                     md.LastPos = curMapData.Pos;
179                                     OpenList.Add(md);
180                                 }
181                             }
182                         }
183                     }
184                     #endregion
185 
186                     #region 左方
187                     {
188                         Position pos1 = new Position(pos.X - 1, pos.Y);
189                         if (CheckMap(pos1))
190                         {
191                             if (CheckInCloseList(pos1) == false)
192                             {
193                                 AStarMapData md = map.GetMapData(pos1);
194                                 if (CheckInOpenList(pos1) == true)
195                                 {
196                                     if (md.F > curMapData.F + 10)
197                                     {
198                                         md.G = curMapData.F + 10;
199                                         md.H = Cal_H(pos1, EndPos);
200                                         md.F = md.G + md.H;
201                                         md.LastPos = curMapData.Pos;
202                                     }
203                                 }
204                                 else
205                                 {
206                                     md.G = curMapData.F + 10;
207                                     md.H = Cal_H(pos1, EndPos);
208                                     md.F = md.G + md.H;
209                                     md.LastPos = curMapData.Pos;
210                                     OpenList.Add(md);
211                                 }
212                             }
213                         }
214                     }
215                     #endregion
216 
217                     #region 右方
218                     {
219                         Position pos1 = new Position(pos.X + 1, pos.Y);
220                         if (CheckMap(pos1))
221                         {
222                             if (CheckInCloseList(pos1) == false)
223                             {
224                                 AStarMapData md = map.GetMapData(pos1);
225                                 if (CheckInOpenList(pos1) == true)
226                                 {
227                                     if (md.F > curMapData.F + 10)
228                                     {
229                                         md.G = curMapData.F + 10;
230                                         md.H = Cal_H(pos1, EndPos);
231                                         md.F = md.G + md.H;
232                                         md.LastPos = curMapData.Pos;
233                                     }
234                                 }
235                                 else
236                                 {
237                                     md.G = curMapData.F + 10;
238                                     md.H = Cal_H(pos1, EndPos);
239                                     md.F = md.G + md.H;
240                                     md.LastPos = curMapData.Pos;
241                                     OpenList.Add(md);
242                                 }
243                             }
244                         }
245                     }
246                     #endregion
247 
248                     #region 左下方
249                     {
250                         Position pos1 = new Position(pos.X - 1, pos.Y + 1);
251                         if (CheckMap(pos1))
252                         {
253                             if (CheckInCloseList(pos1) == false)
254                             {
255                                 AStarMapData md = map.GetMapData(pos1);
256                                 if (CheckInOpenList(pos1) == true)
257                                 {
258                                     if (md.F > curMapData.F + 14)
259                                     {
260                                         md.G = curMapData.F + 14;
261                                         md.H = Cal_H(pos1, EndPos);
262                                         md.F = md.G + md.H;
263                                         md.LastPos = curMapData.Pos;
264                                     }
265                                 }
266                                 else
267                                 {
268                                     md.G = curMapData.F + 14;
269                                     md.H = Cal_H(pos1, EndPos);
270                                     md.F = md.G + md.H;
271                                     md.LastPos = curMapData.Pos;
272                                     OpenList.Add(md);
273                                 }
274                             }
275                         }
276                     }
277                     #endregion
278 
279                     #region 下方
280                     {
281                         Position pos1 = new Position(pos.X, pos.Y + 1);
282                         if (CheckMap(pos1))
283                         {
284                             if (CheckInCloseList(pos1) == false)
285                             {
286                                 AStarMapData md = map.GetMapData(pos1);
287                                 if (CheckInOpenList(pos1) == true)
288                                 {
289                                     if (md.F > curMapData.F + 10)
290                                     {
291                                         md.G = curMapData.F + 10;
292                                         md.H = Cal_H(pos1, EndPos);
293                                         md.F = md.G + md.H;
294                                         md.LastPos = curMapData.Pos;
295                                     }
296                                 }
297                                 else
298                                 {
299                                     md.G = curMapData.F + 10;
300                                     md.H = Cal_H(pos1, EndPos);
301                                     md.F = md.G + md.H;
302                                     md.LastPos = curMapData.Pos;
303                                     OpenList.Add(md);
304                                 }
305                             }
306                         }
307                     }
308                     #endregion
309 
310                     #region 右下方
311                     {
312                         Position pos1 = new Position(pos.X + 1, pos.Y + 1);
313                         if (CheckMap(pos1))
314                         {
315                             if (CheckInCloseList(pos1) == false)
316                             {
317                                 AStarMapData md = map.GetMapData(pos1);
318                                 if (CheckInOpenList(pos1) == true)
319                                 {
320                                     if (md.F > curMapData.F + 14)
321                                     {
322                                         md.G = curMapData.F + 14;
323                                         md.H = Cal_H(pos1, EndPos);
324                                         md.F = md.G + md.H;
325                                         md.LastPos = curMapData.Pos;
326                                     }
327                                 }
328                                 else
329                                 {
330                                     md.G = curMapData.F + 14;
331                                     md.H = Cal_H(pos1, EndPos);
332                                     md.F = md.G + md.H;
333                                     md.LastPos = curMapData.Pos;
334                                     OpenList.Add(md);
335                                 }
336                             }
337                         }
338                     }
339                     #endregion
340 
341 
342                     if (CloseList.Count == 0)
343                     {
344                         res = 1;
345                         break;
346                     }
347 
348                 }
349             }
350 
351             if (res == 0)
352             {
353                 AStarMapData last = map.GetMapData(EndPos);
354                 Result.Add(last.Pos);
355                 while (true)
356                 {
357                     last = map.GetMapData(last.LastPos);
358                     Result.Add(last.Pos);
359                     if (last.Pos == StartPos)
360                         break;
361 
362                 }
363                 Result.Reverse();
364             }
365             return res;
366         }
367     }
368 }

 

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace A星算法
 8 {
 9     class AStarMapData
10     {
11         public int 地图;
12         public int 类型;
13         public Position Pos;
14         public Position LastPos;
15         public int G;
16         public int H;
17         public int F;
18 
19 
20         public AStarMapData(int x, int y)
21         {
22             Pos = new Position(x, y);
23             LastPos = null;
24             G = 0;
25             H = 0;
26             F = 0;
27             地图 = 0;
28             类型 = 0;
29         }
30     }
31 
32 }

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace A星算法
 8 {
 9     class AStarMap
10     {
11         public int Width;
12         public int Height;
13         public List<AStarMapData> MapData;
14 
15 
16         public AStarMap(int width, int height)
17         {
18             Width = width;
19             Height = height;
20             MapData = new List<AStarMapData>();
21             for (int y = 0; y < Height; y++)
22                 for (int x = 0; x < Width; x++)
23                 {
24                     MapData.Add(new AStarMapData(x, y));
25                 }
26         }
27 
28         public AStarMap(int widht, int height, List<AStarMapData> mapdata)
29         {
30             Width = widht;
31             Height = height;
32             MapData = mapdata;
33         }
34 
35         public AStarMapData GetMapData(Position pos)
36         {
37             return MapData[pos.Y * Width + pos.X];
38         }
39 
40         //public AStarMapData GetMapData(int x, int y)
41         //{
42         //    return MapData[y * Width + x];
43         //}
44 
45         public int GetMapType(Position pos)
46         {
47             return MapData[pos.Y * Width + pos.X].类型;
48         }
49 
50         public void SetMapType(Position pos, int value)
51         {
52             MapData[pos.Y * Width + pos.X].类型 = value;
53         }
54 
55         //public int GetMapType(int x, int y)
56         //{
57         //    return MapData[y * Width + x].类型;
58         //}
59 
60         //public void SetMapType(int x, int y, int value)
61         //{
62         //    MapData[y * Width + x].类型 = value;
63         //}
64 
65     }
66 }

 

有趣的A*算法演示

标签:

原文地址:http://www.cnblogs.com/ErrorGz/p/4525192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!