四种算法是DFS,BFS,Heuristic DFS, Heuristic BFS (A*) 用了两张障碍表,一张是典型的迷宫:
char Block[SY][SX]= {{1,1,1,1,1,1,1,1,1,1,1 }, {1,0,1,0,1,0,0,0,0,0,1 }, {1,0,1,0,0,0,1,0,1,1,1 }, {1,0,0,0,1,0,1,0,0,0,1 }, {1,0,1,1,0,0,1,0,0,1,1 }, {1,0,1,0,1,1,0,1,0,0,1 }, {1,0,0,0,0,0,0,0,1,0,1 }, {1,0,1,0,1,0,1,0,1,0,1 }, {1,0,0,1,0,0,1,0,1,0,1 }, {1,1,1,1,1,1,1,1,1,1,1 }};
第二张是删掉一些障碍后的:
char Block[SY][SX]= {{1,1,1,1,1,1,1,1,1,1,1 }, {1,0,1,0,1,0,0,0,0,0,1 }, {1,0,1,0,0,0,1,0,1,1,1 }, {1,0,0,0,0,0,1,0,0,0,1 }, {1,0,0,1,0,0,1,0,0,1,1 }, {1,0,1,0,0,1,0,1,0,0,1 }, {1,0,0,0,0,0,0,0,1,0,1 }, {1,0,1,0,0,0,1,0,1,0,1 }, {1,0,0,1,0,0,1,0,0,0,1 }, {1,1,1,1,1,1,1,1,1,1,1 }};
结果: 尝试节点数 合法节点数 步数 深度优先 416/133 110/43 19/25 广度优先 190/188 48/49 19/15 深度+启发 283/39 82/22 19/19 广度+启发 189/185 48/49 19/15
所以可以看出深度+启发是最好的,效率高路径也挺短。A*第一是不真实二是慢三是空间消耗较大。
001 |
#include <iostream.h> |
008 |
int dx[ 4 ]={ 0 , 0 ,- 1 , 1 }; |
009 |
int dy[ 4 ]={- 1 , 1 , 0 , 0 }; |
024 |
{{ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 }, |
025 |
{ 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 }, |
026 |
{ 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 1 }, |
027 |
{ 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 }, |
028 |
{ 1 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 1 }, |
029 |
{ 1 , 0 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 1 }, |
030 |
{ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 }, |
031 |
{ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 }, |
032 |
{ 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 }, |
033 |
{ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 }}; |
042 |
int TargetX= 9 ,TargetY= 8 ; |
052 |
memset(Act, 0 ,sizeof(Act)); |
053 |
memset(Table, 0 ,sizeof(Table)); |
057 |
Level++;LevelComplete= 0 ; |
058 |
while (!LevelComplete) |
060 |
Act[Level]=GetNextAct( ); |
061 |
if (Act[Level]<=MaxAct) |
071 |
if (Act[Level]>MaxAct) |
074 |
LevelComplete=AllComplete= 1 ; |
082 |
if ((x==TargetX)&&(y==TargetY)) |
084 |
for ( int i= 0 ;i<=Level;i++) |
087 |
cout<<Level+ 1 << " " <<sum1<< " " <<sum2<<endl; |
088 |
LevelComplete=AllComplete= 1 ; |
094 |
int tx=x+dx[Act[Level]- 1 ]; |
095 |
int ty=y+dy[Act[Level]- 1 ]; |
096 |
if (Act[Level]>MaxAct) |
098 |
if ((tx>=SX)||(tx< 0 )) |
100 |
if ((ty>=SY)||(ty< 0 )) |
102 |
if (Table[ty][tx]== 1 ) |
104 |
if (Block[ty][tx]== 1 ) |
114 |
x-=dx[Act[Level- 1 ]- 1 ]; |
115 |
y-=dy[Act[Level- 1 ]- 1 ]; |
128 |
for ( int i= 0 ;i< 4 ;i++) |
129 |
dis[i]=abs(x+dx[i]-TargetX)+abs(y+dy[i]-TargetY); |
140 |
if ((dis[i]==t)&&(i!=(order[ 0 ]- 1 ))) |
164 |
if (Act[Level]==order[ 0 ]) |
166 |
if (Act[Level]==order[ 1 ]) |
168 |
if (Act[Level]==order[ 2 ]) |
170 |
if (Act[Level]==order[ 3 ]) |
|