找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
發表文章前請先閱讀相關版規尊貴會員無限使用任何功能你準備好成為出色的版主了嗎?
明日花鬼父進擊的巨3dvrrpg按摩
knmb 066midv 274proud fasply 015攝影h?英知cand 118

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

(4月新番)[繁]為美好

✡ 斗破蒼穹 年番/鬥

桃園觀音文林路 女子

[繁]迷宮飯16-

[繁]無職轉生 第二季1

[繁]轉生貴族憑鑑定技
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 3850|回復: 2
打印上一主題下一主題

[問題]A-Star 演算法 與 STL List問題 關閉[複製鏈接]

  中學生(1000/4000)

唷吼吼齁

Rank: 3Rank: 3Rank: 3

帖子
153
積分
1615 點
潛水值
13857 米
跳轉到指定樓層
樓主
發表於 2011-10-1 01:47 AM|只看該作者|倒序瀏覽
若有安裝色情守門員,可用無界、自由門等軟件瀏覽伊莉。或使用以下網址瀏覽伊莉: http://www.eyny.com:81/index.php
本帖最後由 ahijup 於 2011-10-1 02:11 AM 編輯

小弟最近寫的A-star走迷宮配合 STL 的 List以下是我的程式碼 :

mClasses.h
  1. /*自定義函式宣告*/
  2. #include <iostream>
  3. #include <list>
  4. #include <math.h>
  5. #include <algorithm>
  6. using namespace std;
  7. enum Direction {
  8.         Right , Left , UP , Down , RD , LD , LU , RU
  9. } ;
  10. enum Way {
  11.         FourWay = 2 , eightWay = 1
  12. } ;


  13. class mPoint {
  14. public:
  15.         int x , y ;
  16.         Direction dir ;
  17.         double h , f ;
  18.         mPoint *ParentNode ;
  19.         bool operator == (mPoint x ) ;
  20. } ;
  21. //定義出一個list裡面全部都包含mPoint
  22. typedef list<mPoint> POINTList ;
  23. //用list印出元素的函式呼叫
  24. void print_list( mPoint x ) ;
  25. //排序的比較方法
  26. bool Comparer1 ( mPoint x , mPoint y ) ;
  27. struct mSize {
  28.         int width , height ;
  29. } ;

  30. class AStarSearch {
  31. private:
  32.         mSize mapSize ;
  33.         mPoint startPT , endPT ;
  34.         unsigned short **map ;
  35. public :
  36.         /*        初始化這個類別 , mapSize -> 這個地圖的大小  , map -> 搜尋的地圖檔        */
  37.         AStarSearch ( mSize mapSize  , unsigned short **map ) {
  38.                 this->mapSize = mapSize ;
  39.                 this->map = map ;
  40.         } ;
  41.         /*        開始搜尋路徑 , startPT -> 起始座標點 , endPT -> 結束點 , isShifted -> 是否把點座標轉換成2維陣列的座標 ,         */
  42.         mPoint * SearchRoute ( mPoint startPT , mPoint endPT , bool isShifted , Way w );
  43.         ~AStarSearch() ;
  44. } ;
複製代碼
mClasses.cpp
  1. /*mClasses.h中實作方法*/
  2. #include "mClasses.h"
  3. void print_list( mPoint x ) {
  4.         cout << x.x << " , " << x.y << " " ;
  5. } ;
  6. bool Comparer1 ( mPoint x , mPoint y ) {
  7.         if ( x.h < y.h )
  8.                 return 1 ;
  9.         else               
  10. return 0 ;
  11. } ;


  12. bool mPoint::operator == (mPoint x ) {
  13.         if ( (this->x == x.x) && (this->y == x.y) )
  14.                 return true ;
  15.         return false ;
  16. } ;


  17. mPoint * AStarSearch::SearchRoute(mPoint startPT , mPoint endPT , bool isShifted , Way w) {
  18.                 this->startPT = startPT ;
  19.                 this->endPT = endPT ;

  20.                 if ( !isShifted ) {
  21.                         this->startPT.x += (int) (this->mapSize.width / 2) ;
  22.                         this->startPT.y = (int) (this->mapSize.height / 2) - this->startPT.y ;
  23.                         this->endPT.x += (int) (this->mapSize.width / 2) - 1 ;
  24.                         this->endPT.y = (int) (this->mapSize.height / 2) - this->endPT.y - 1 ;
  25.                 }

  26.                 if ( this->map[this->startPT.x][this->startPT.y] == 1 ) {
  27.                         return NULL ;
  28.                 } else {
  29.                         this->startPT.f = 0.0 ;
  30.                         this->startPT.ParentNode = NULL ;
  31.                         POINTList r  ;
  32.                         r.push_back( this->startPT ) ;
  33.                         POINTList closeList ;
  34.                         while ( !r.empty() ) {
  35.                                 mPoint * ExtendPoint = &(mPoint) *r.begin( ) ;
  36.                                 //把最前面的資料pop出來
  37.                                 //r.pop_front( ) ;
  38.                                 r.remove( *ExtendPoint) ;
  39.                                 //cout << "current pt -> " << ExtendPoint.x << " , " << ExtendPoint.y << endl ;
  40.                                 //如果找到
  41.                                 if ( *ExtendPoint == this->endPT ) {
  42.                                                         return ExtendPoint ;
  43.                                 }
  44.                                 closeList.push_back(*ExtendPoint) ;
  45.                                 closeList.sort( Comparer1 ) ;
  46.                                 //8 - way Search
  47.                                 mPoint x[8] ;
  48.                                 x[0].x = (*ExtendPoint).x + 1 ;

  49.                                 x[0].y = (*ExtendPoint).y ;
  50.                                 x[0].dir = Direction::Right ;

  51.                                 x[1].x = (*ExtendPoint).x + 1 ;
  52.                                 x[1].y = (*ExtendPoint).y + 1 ;
  53.                                 x[1].dir = Direction::RD ;
  54.                                 x[2].x = (*ExtendPoint).x  ;
  55.                                 x[2].y = (*ExtendPoint).y + 1;
  56.                                 x[2].dir = Direction::Down ;
  57.                                 x[3].x = (*ExtendPoint).x - 1 ;
  58.                                 x[3].y = (*ExtendPoint).y + 1 ;
  59.                                 x[3].dir = Direction::LD ;

  60.                                 x[4].x = (*ExtendPoint).x - 1 ;
  61.                                 x[4].y = (*ExtendPoint).y ;
  62.                                 x[4].dir = Direction::Left ;
  63.                                 x[5].x = (*ExtendPoint).x - 1 ;

  64.                                 x[5].y = (*ExtendPoint).y - 1 ;
  65.                                 x[5].dir = Direction::LU ;
  66.                                 x[6].x = (*ExtendPoint).x ;
  67.                                 x[6].y = (*ExtendPoint).y - 1 ;
  68.                                 x[6].dir = Direction::UP ;

  69.                                 x[7].x = (*ExtendPoint).x + 1 ;
  70.                                 x[7].y = (*ExtendPoint).y - 1 ;
  71.                                 x[7].dir = Direction::RU ;

  72.                                 for ( int a = 0 ; a < 8 ; a += w ) {
  73.                                         if ( ( x[a].x >= 0 && x[a].x < this->mapSize.height ) && ( x[a].y >= 0 && x[a].y < this->mapSize.width ) ) {                                                                        if ( !this->map[ x[a].y ][ x[a].x ] && !(x[a] == *ExtendPoint) ) {
  74.                                                         x[a].f = (*ExtendPoint).f + 1.0 ;
  75.                                                         x[a].h = x[a].f + sqrt( pow( (double)(endPT.x - x[a].x) , 2 ) + pow( (double)(endPT.y - x[a].y) , 2) ) ;                                                                //x[a].h = x[a].f + abs(endPT.x - x[a].x) + abs(endPT.y - x[a].y) ;
  76.                                                         //if n' in OPEN list and new n' is not better, continue
  77.                                                         //POINTList::iterator iter = lower_bound(r.begin() , r.end() , x[a] , Comparer1 ) ;
  78.                                                         mPoint *tmp = NULL ;

  79.                                                         POINTList::iterator iter = find(r.begin() , r.end() , x[a] ) ;

  80.                                                         if ( iter != r.end() ) {
  81.                                                                         tmp = & (mPoint) *iter ;
  82.                                                                         if ( tmp->h < x[a].h )
  83.                                                                                 continue ;
  84.                                                          }                                                      
  85.                                                         //if n' in CLOSED list and new n' is not better, continue
  86.                                                         mPoint *tmp1 = NULL ;

  87.                                                         iter = find(closeList.begin() , closeList.end() , x[a] ) ;
  88.                                                         if ( iter != closeList.end() ) {
  89.                                                                         tmp1 = & (mPoint) *iter ;
  90.                                                                         if ( tmp1->h < x[a].h )
  91.                                                                                 continue ;
  92.                                                         }
  93.                                                         //remove any n' from OPEN and CLOSED

  94.                                                         //r.remove(x[a]) ;
  95.                                                         //closeList.remove( x[a] ) ;

  96.                                                         //add n as n's parent
  97.                                                         x[a].ParentNode = ExtendPoint ;

  98.                                                         //add n' to OPEN
  99.                                                         if ( tmp != NULL ) {
  100.                                                                 tmp = &x[a] ;
  101.                                                         } else {
  102.                                                                 r.push_back( x[a] ) ;
  103.                                                         }
  104.                                                         if ( tmp1 != NULL )
  105.                                                                 closeList.remove( *tmp1 ) ;

  106.                                                         r.sort( Comparer1 ) ;

  107.                                                      }
  108.                                         }
  109.                                 }
  110.                 }
  111.         return NULL ;
  112. }
  113. } ;
複製代碼







main.cpp
  1. #include <mClasses.h>
  2. void main() {
  3.         int x = 50 , y  = 50 , i ;
  4.         //動態宣告出2-dim陣列
  5.         unsigned short **input = new unsigned short* [ x ] ;

  6.         for ( i = 0 ; i < x ; i ++ )
  7.                 input[i] = new unsigned short[y] ;
  8.         mSize size;

  9.         size.height = x  ;
  10.         size.width = y ;

  11.         for ( i = 0 ; i < x ; i++ ) {
  12.                 for ( int j = 0 ; j < y ; j++ )
  13.                         input[i][j] = 0 ;
  14.         }

  15.         AStarSearch *ass = new AStarSearch( shiftSize , input ) ;

  16.         mPoint start ;
  17.         start.x = 0 ;
  18.         start.y = 0 ;

  19.         mPoint end ;
  20.         end.x = 25 ;
  21.         end.y = 20 ;

  22.         mPoint *r = ass->SearchRoute( start , end , true , Way::FourWay) ;

  23.         return;
  24. }
複製代碼
問題1: 在main.cpp中回傳結果的 *r , ParentNode 不知道為啥咪會一直指向目標點 ( end ) 。
問題2: 在A-star搜尋路徑的時候,好像有拓展過頭的現象,執行時間會非常的久。

A-star 演算法我是參考 :
下載: 訪客無法瀏覽下載點,請先 註冊登入會員


編譯器 : vc++ 2008...
瀏覽完整內容,請先 註冊登入會員
分享分享0收藏收藏0支持支持0
若對尊貴或贊助會員有任何疑問,歡迎向我們查詢。我們的即時通或MSN: admin@eyny.com

使用道具檢舉

Rank: 2Rank: 2

帖子
85
積分
598 點
潛水值
21654 米
頭香
發表於 2011-10-2 02:44 PM|只看該作者
如果瀏覽伊莉時速度太慢或無法連接,可以使用其他分流瀏覽伊莉,www01.eyny.com(02,03)。
你每次取節點有使用HEAP之類的優先隊列嗎?
這樣每次取g()+h()的最小值的點只要O(logn)

使用道具檢舉

Rank: 1

帖子
134
積分
106 點
潛水值
3720 米
3
發表於 2014-1-7 01:10 PM|只看該作者
分享使你變得更實在,可以使其他人感到快樂,分享是我們的動力。今天就來分享你的資訊、圖片或檔案吧。
STL檔案格式有點複雜
我最近也在找相關資料 但完全沒有詳細的資料可以參考 ><

點評

snowflying 不好意思,這是舊帖唷,而且文中指的 STL 是 Standard Template Library  發表於 2014-1-7 03:59 PM
若有安裝色情守門員,可用無界、自由門等軟件瀏覽伊莉。或使用以下網址瀏覽伊莉: http://www.eyny.com:81/index.php

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部