小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

[游戲開發(fā)]四種尋路算法并比較 - 開發(fā)資源區(qū) - 中國(guó)移動(dòng)開發(fā)者社區(qū)論壇 OMS開發(fā),A...

 LyNyas 2010-07-06

[其他] [游戲開發(fā)]四種尋路算法并比較

四種算法是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> 
002 #include <memory.h> 
003 #include <stdlib.h> 
004   
005 #define SX 11 //寬 
006 #define SY 10 //長(zhǎng) 
007   
008 int dx[4]={0,0,-1,1}; //四種移動(dòng)方向?qū)和y坐標(biāo)的影響 
009 int dy[4]={-1,1,0,0}; 
010   
011 /*char Block[SY][SX]= //障礙表 
012 {{ 1,1,1,1,1,1,1,1,1,1,1 }, 
013 { 1,0,1,0,1,0,0,0,0,0,1 }, 
014 { 1,0,1,0,0,0,1,0,1,1,1 }, 
015 { 1,0,0,0,0,0,1,0,0,0,1 }, 
016 { 1,0,0,1,0,0,1,0,0,1,1 }, 
017 { 1,0,1,0,0,1,0,1,0,0,1 }, 
018 { 1,0,0,0,0,0,0,0,1,0,1 }, 
019 { 1,0,1,0,0,0,1,0,1,0,1 }, 
020 { 1,0,0,1,0,0,1,0,0,0,1 }, 
021 { 1,1,1,1,1,1,1,1,1,1,1 }};*/ 
022   
023 char Block[SY][SX]= //障礙表 
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 }}; 
034   
035 int MaxAct=4; //移動(dòng)方向總數(shù) 
036 char Table[SY][SX]; //已到過(guò)標(biāo)記 
037 int Level=-1; //第幾步 
038 int LevelComplete=0; //這一步的搜索是否完成 
039 int AllComplete=0; //全部搜索是否完成 
040 char Act[1000]; //每一步的移動(dòng)方向,搜索1000步,夠了吧? 
041 int x=1,y=1; //現(xiàn)在的x和y坐標(biāo) 
042 int TargetX=9,TargetY=8; //目標(biāo)x和y坐標(biāo) 
043 int sum1=0,sum2=0
044   
045 void Test( ); 
046 void Back( ); 
047 int ActOK( ); 
048 int GetNextAct( ); 
049   
050 void main( ) 
051
052 memset(Act,0,sizeof(Act)); //清零 
053 memset(Table,0,sizeof(Table)); 
054 Table[y][x]=1; //做已到過(guò)標(biāo)記 
055 while (!AllComplete) //是否全部搜索完 
056
057 Level++;LevelComplete=0; //搜索下一步 
058 while (!LevelComplete) 
059
060 Act[Level]=GetNextAct( ); //改變移動(dòng)方向 
061 if (Act[Level]<=MaxAct) 
062 sum1++; 
063 if (ActOK( )) //移動(dòng)方向是否合理 
064
065 sum2++; 
066 Test( ); //測(cè)試是否已到目標(biāo) 
067 LevelComplete=1; //該步搜索完成 
068
069 else 
070
071 if (Act[Level]>MaxAct) //已搜索完所有方向 
072      Back( ); //回上一步 
073 if (Level<0) //全部搜索完仍無(wú)結(jié)果 
074     LevelComplete=AllComplete=1; //退出 
075
076
077
078
079   
080 void Test( ) 
081
082 if ((x==TargetX)&&(y==TargetY)) //已到目標(biāo) 
083
084 for (int i=0;i<=Level;i++) 
085 cout<<(int)Act[i]; //輸出結(jié)果 
086 cout<<endl; 
087 cout<<Level+1<<" "<<sum1<<" "<<sum2<<endl; 
088 LevelComplete=AllComplete=1; //完成搜索 
089
090
091   
092 int ActOK( ) 
093
094 int tx=x+dx[Act[Level]-1]; //將到點(diǎn)的x坐標(biāo) 
095 int ty=y+dy[Act[Level]-1]; //將到點(diǎn)的y坐標(biāo) 
096 if (Act[Level]>MaxAct) //方向錯(cuò)誤? 
097 return 0
098 if ((tx>=SX)||(tx<0)) //x坐標(biāo)出界? 
099 return 0
100 if ((ty>=SY)||(ty<0)) //y坐標(biāo)出界? 
101 return 0
102 if (Table[ty][tx]==1) //已到過(guò)? 
103 return 0
104 if (Block[ty][tx]==1) //有障礙? 
105 return 0
106 x=tx; 
107 y=ty; //移動(dòng) 
108 Table[y][x]=1; //做已到過(guò)標(biāo)記 
109 return 1
110
111   
112 void Back( ) 
113
114 x-=dx[Act[Level-1]-1]; 
115 y-=dy[Act[Level-1]-1]; //退回原來(lái)的點(diǎn) 
116 Table[y][x]=0; //清除已到過(guò)標(biāo)記 
117 Act[Level]=0; //清除方向 
118 Level--; //回上一層 
119
120   
121 int GetNextAct( ) //找到下一個(gè)移動(dòng)方向。這一段程序有些亂, 
122 //仔細(xì)看! 
123
124 int dis[4]; 
125 int order[4]; 
126 int t=32767
127 int tt=2
128 for (int i=0;i<4;i++) 
129 dis[i]=abs(x+dx[i]-TargetX)+abs(y+dy[i]-TargetY); 
130 for (i=0;i<4;i++) 
131 if (dis[i]<t) 
132
133 order[0]=i+1
134 t=dis[i]; 
135
136 if (Act[Level]==0
137 return order[0]; 
138 order[1]=-1
139 for (i=0;i<4;i++) 
140 if ((dis[i]==t)&&(i!=(order[0]-1))) 
141
142 order[1]=i+1
143 break
144
145 if (order[1]!=-1
146
147 for (i=0;i<4;i++) 
148 if (dis[i]!=t) 
149
150 order[tt]=i+1
151 tt++; 
152
153
154 else 
155
156 for (i=0;i<4;i++) 
157 if (dis[i]!=t) 
158
159 order[tt-1]=i+1
160 tt++; 
161
162
163   
164 if (Act[Level]==order[0]) 
165 return order[1]; 
166 if (Act[Level]==order[1]) 
167 return order[2]; 
168 if (Act[Level]==order[2]) 
169 return order[3]; 
170 if (Act[Level]==order[3]) 
171 return 5
172 }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多