四種算法是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 }};
結(jié)果: 嘗試節(jié)點(diǎn)數(shù) 合法節(jié)點(diǎn)數(shù) 步數(shù) 深度優(yōu)先 416/133 110/43 19/25 廣度優(yōu)先 190/188 48/49 19/15 深度+啟發(fā) 283/39 82/22 19/19 廣度+啟發(fā) 189/185 48/49 19/15
所以可以看出深度+啟發(fā)是最好的,效率高路徑也挺短。A*第一是不真實(shí)二是慢三是空間消耗較大。
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]) |
|