資源簡介
a星算法matlab實現 還有c++實現的算法

代碼片段和文件信息
%function?astar(mapdataNode)
tic
%=============數據=====================
?load?(‘map.mat‘);
?map?=map([1:10][1:10]);
%??block?=?[??
%?????0110000110;??
%?????0110001000;??
%?????0000001110;?
%?????0000000000;??
%?????0000000000;??
%?????0000000000;??
%?????0000000000;??
%?????0000000000;??
%?????0000000000;??
%?????0000000000;??
%?];
%?map?=?block;
map=fliplr(map‘)
startNode=[5?5];
endNode?=[7?7];
%endNode?=[9?10];
dataNode=[startNode;endNode]
%================================
m?=?10;n?=?10;%表示不含四周墻的地圖?行數??列數?
?Spoint?=?[2?2];%?開始點
%Spoint?=?[3?3];
Epoint?=?[m+1?n+1];%接受點
?%%//障礙表???
?block?=?[??
????0100000000;??
????0110111000;??
????0000000000;?
????1110100000;??
????0100101110;??
????0100111110;??
????0001100010;??
????0100001010;??
????0111011011;??
????0000001000;??
];?
%========map?判空是否有效========
??[m?n]?=?size(map);
?if?([m?n]?~=?[0?0])
??block?=map;
??m?=?m;
??n?=?n;%表示不含四周墻的地圖?行數??列數?
?else
?????if([dataNode(21)?dataNode(22)]?>?[m?n])
????????dataNode(2:)=?[m?n]
?????end
?end
?Spoint?=?dataNode(1:)+1;%?開始點
?Epoint?=dataNode(2:)+1;%接受點
%%地圖
for?i?=?1:m+2%?表示加了左右的墻
????if?i?==?1
????????for?j?=?1:n+2
????????????Matrix(ij)?=?-inf;%墻
????????end
????elseif?i?==?m+2
????????for?j?=?1:n+2%墻
????????????Matrix(ij)?=?-inf;
????????end
????else
????????for?j?=?1:n+2
????????????if?((j?==?1)|(j?==?n+2))
????????????????Matrix(ij)?=?-inf;%墻
????????????else
????????????????Matrix(ij)?=?inf;%其他都可以?表示可以走的路,但?損耗值還沒計算,先用無窮表示
????????????end
????????end
????end
end
%%向地圖添加障障礙
%Matrix?=?zeros(size(block));
?for?i?=?1:m
?????for?j=1:n
??????if(block(ij)==1)
?????????Matrix(i+1j+1)=-inf;%??這是四周加墻的緣故
??????elseif(block(ij)==0)
??????????Matrix(i+1j+1)=inf;%?
??????else
??????????Matrix(i+1j+1)=inf;%?
??????end
?????end
?end
????subplot(131);
????plot(Spoint(1)Spoint(2)‘r+‘);
%%尋路
Matrix(Spoint(1)Spoint(2))=0;
Matrix(Epoint(1)Epoint(2))=inf;
G=Matrix;%計算值G
F=Matrix;%F
openlist=Matrix;
closelist=Matrix;
parentx=Matrix;
parenty=Matrix;
openlist(Spoint(1)Spoint(2))?=0;
%closelist(Epoint(1)Epoint(2))=inf;
%畫圖
for?i?=?1:n+2
????for?j?=?1:m+2
????????k?=?Matrix(ij);
????????if(k?==?-inf)
????????????subplot(131);
????????????plot(ij‘r.‘);
????????elseif(k?==?inf)
????????????subplot(131);
????????????plot(ij‘gh‘);
????????else
????????????subplot(131);
????????????plot(ij‘gh‘);
????????end
????????hold?on
????end
end
title(‘A*‘);
axis([0?m+3?0?n+3]);
subplot(131);
plot(Epoint(1)Epoint(2)‘b+‘);
subplot(131);
plot(Spoint(1)Spoint(2)‘b+‘);
while(1)%?循環找到路徑
????num=inf;%當然消耗值
????for?p=1:m+2
????????for?q=1:n+2
????????????if(openlist(pq)==0&&closelist(pq)~=1)%如果在開啟列表并沒在關閉列表中
????????????????Outpoint=[pq];%第一次?起始
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????7723??2017-04-27?11:19??astar.m
?????文件????????719??2017-04-23?19:23??map.mat
?????文件?????186720??2017-04-21?11:32??AStar.zip
-----------?---------??----------?-----??----
???????????????195162????????????????????3
- 上一篇:基于C語言的快速傅里葉變換FFT算法含詳細注釋
- 下一篇:數據結構編程題目及答案
評論
共有 條評論