- 最後登錄
- 2024-2-3
- 在線時間
- 1 小時
- 註冊時間
- 2009-5-25
- 閱讀權限
- 30
- 精華
- 0
- UID
- 6440807
- 帖子
- 153
- 積分
- 1615 點
- 潛水值
- 13857 米
| 若有安裝色情守門員,可用無界、自由門等軟件瀏覽伊莉。或使用以下網址瀏覽伊莉: http://www.eyny.com:81/index.php 本帖最後由 ahijup 於 2011-10-1 02:11 AM 編輯
小弟最近寫的A-star走迷宮配合 STL 的 List以下是我的程式碼 :
mClasses.h- /*自定義函式宣告*/
- #include <iostream>
- #include <list>
- #include <math.h>
- #include <algorithm>
- using namespace std;
- enum Direction {
- Right , Left , UP , Down , RD , LD , LU , RU
- } ;
- enum Way {
- FourWay = 2 , eightWay = 1
- } ;
- class mPoint {
- public:
- int x , y ;
- Direction dir ;
- double h , f ;
- mPoint *ParentNode ;
- bool operator == (mPoint x ) ;
- } ;
- //定義出一個list裡面全部都包含mPoint
- typedef list<mPoint> POINTList ;
- //用list印出元素的函式呼叫
- void print_list( mPoint x ) ;
- //排序的比較方法
- bool Comparer1 ( mPoint x , mPoint y ) ;
- struct mSize {
- int width , height ;
- } ;
- class AStarSearch {
- private:
- mSize mapSize ;
- mPoint startPT , endPT ;
- unsigned short **map ;
- public :
- /* 初始化這個類別 , mapSize -> 這個地圖的大小 , map -> 搜尋的地圖檔 */
- AStarSearch ( mSize mapSize , unsigned short **map ) {
- this->mapSize = mapSize ;
- this->map = map ;
- } ;
- /* 開始搜尋路徑 , startPT -> 起始座標點 , endPT -> 結束點 , isShifted -> 是否把點座標轉換成2維陣列的座標 , */
- mPoint * SearchRoute ( mPoint startPT , mPoint endPT , bool isShifted , Way w );
- ~AStarSearch() ;
- } ;
複製代碼 mClasses.cpp
- /*mClasses.h中實作方法*/
- #include "mClasses.h"
- void print_list( mPoint x ) {
- cout << x.x << " , " << x.y << " " ;
- } ;
- bool Comparer1 ( mPoint x , mPoint y ) {
- if ( x.h < y.h )
- return 1 ;
- else
- return 0 ;
- } ;
- bool mPoint::operator == (mPoint x ) {
- if ( (this->x == x.x) && (this->y == x.y) )
- return true ;
- return false ;
- } ;
- mPoint * AStarSearch::SearchRoute(mPoint startPT , mPoint endPT , bool isShifted , Way w) {
- this->startPT = startPT ;
- this->endPT = endPT ;
- if ( !isShifted ) {
- this->startPT.x += (int) (this->mapSize.width / 2) ;
- this->startPT.y = (int) (this->mapSize.height / 2) - this->startPT.y ;
- this->endPT.x += (int) (this->mapSize.width / 2) - 1 ;
- this->endPT.y = (int) (this->mapSize.height / 2) - this->endPT.y - 1 ;
- }
- if ( this->map[this->startPT.x][this->startPT.y] == 1 ) {
- return NULL ;
- } else {
- this->startPT.f = 0.0 ;
- this->startPT.ParentNode = NULL ;
- POINTList r ;
- r.push_back( this->startPT ) ;
- POINTList closeList ;
- while ( !r.empty() ) {
- mPoint * ExtendPoint = &(mPoint) *r.begin( ) ;
- //把最前面的資料pop出來
- //r.pop_front( ) ;
- r.remove( *ExtendPoint) ;
- //cout << "current pt -> " << ExtendPoint.x << " , " << ExtendPoint.y << endl ;
- //如果找到
- if ( *ExtendPoint == this->endPT ) {
- return ExtendPoint ;
- }
- closeList.push_back(*ExtendPoint) ;
- closeList.sort( Comparer1 ) ;
- //8 - way Search
- mPoint x[8] ;
- x[0].x = (*ExtendPoint).x + 1 ;
- x[0].y = (*ExtendPoint).y ;
- x[0].dir = Direction::Right ;
- x[1].x = (*ExtendPoint).x + 1 ;
- x[1].y = (*ExtendPoint).y + 1 ;
- x[1].dir = Direction::RD ;
- x[2].x = (*ExtendPoint).x ;
- x[2].y = (*ExtendPoint).y + 1;
- x[2].dir = Direction::Down ;
- x[3].x = (*ExtendPoint).x - 1 ;
- x[3].y = (*ExtendPoint).y + 1 ;
- x[3].dir = Direction::LD ;
- x[4].x = (*ExtendPoint).x - 1 ;
- x[4].y = (*ExtendPoint).y ;
- x[4].dir = Direction::Left ;
- x[5].x = (*ExtendPoint).x - 1 ;
- x[5].y = (*ExtendPoint).y - 1 ;
- x[5].dir = Direction::LU ;
- x[6].x = (*ExtendPoint).x ;
- x[6].y = (*ExtendPoint).y - 1 ;
- x[6].dir = Direction::UP ;
- x[7].x = (*ExtendPoint).x + 1 ;
- x[7].y = (*ExtendPoint).y - 1 ;
- x[7].dir = Direction::RU ;
- for ( int a = 0 ; a < 8 ; a += w ) {
- 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) ) {
- x[a].f = (*ExtendPoint).f + 1.0 ;
- 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) ;
- //if n' in OPEN list and new n' is not better, continue
- //POINTList::iterator iter = lower_bound(r.begin() , r.end() , x[a] , Comparer1 ) ;
- mPoint *tmp = NULL ;
- POINTList::iterator iter = find(r.begin() , r.end() , x[a] ) ;
- if ( iter != r.end() ) {
- tmp = & (mPoint) *iter ;
- if ( tmp->h < x[a].h )
- continue ;
- }
- //if n' in CLOSED list and new n' is not better, continue
- mPoint *tmp1 = NULL ;
- iter = find(closeList.begin() , closeList.end() , x[a] ) ;
- if ( iter != closeList.end() ) {
- tmp1 = & (mPoint) *iter ;
- if ( tmp1->h < x[a].h )
- continue ;
- }
- //remove any n' from OPEN and CLOSED
- //r.remove(x[a]) ;
- //closeList.remove( x[a] ) ;
- //add n as n's parent
- x[a].ParentNode = ExtendPoint ;
- //add n' to OPEN
- if ( tmp != NULL ) {
- tmp = &x[a] ;
- } else {
- r.push_back( x[a] ) ;
- }
- if ( tmp1 != NULL )
- closeList.remove( *tmp1 ) ;
- r.sort( Comparer1 ) ;
- }
- }
- }
- }
- return NULL ;
- }
- } ;
複製代碼
main.cpp- #include <mClasses.h>
- void main() {
- int x = 50 , y = 50 , i ;
- //動態宣告出2-dim陣列
- unsigned short **input = new unsigned short* [ x ] ;
- for ( i = 0 ; i < x ; i ++ )
- input[i] = new unsigned short[y] ;
- mSize size;
- size.height = x ;
- size.width = y ;
- for ( i = 0 ; i < x ; i++ ) {
- for ( int j = 0 ; j < y ; j++ )
- input[i][j] = 0 ;
- }
- AStarSearch *ass = new AStarSearch( shiftSize , input ) ;
- mPoint start ;
- start.x = 0 ;
- start.y = 0 ;
- mPoint end ;
- end.x = 25 ;
- end.y = 20 ;
- mPoint *r = ass->SearchRoute( start , end , true , Way::FourWay) ;
- return;
- }
複製代碼 問題1: 在main.cpp中回傳結果的 *r , ParentNode 不知道為啥咪會一直指向目標點 ( end ) 。
問題2: 在A-star搜尋路徑的時候,好像有拓展過頭的現象,執行時間會非常的久。
A-star 演算法我是參考 :
編譯器 : vc++ 2008... |
|