A星算法(VC版源码)
生活随笔
收集整理的這篇文章主要介紹了
A星算法(VC版源码)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
源碼不是我寫的,如有侵權,請聯系我,更改版權
使用方法:
對于空地左鍵單擊后會產生障礙,對障礙左鍵單擊會消除障礙,對于起點,兩次左鍵盤單擊會消除起點,如果不存在起點,單擊右鍵會產生起點,如果存在起點不存在終點,單擊右鍵會產生終點,如果既存在起點又存在終點,單擊右鍵會消除終點,點擊開始尋路回畫出路徑
效果圖:
C++源碼:
<pre name="code" class="cpp">#include ".\astart.h" #include "List.cpp" #include <math.h> Astart::Astart(int a[37][64],int w,int l,int s,int e) {for(int i=0; i<l; i++){for(int j=0; j<w; j++){map[i][j] = a[i][j];}}WIDTH = w;start = s;end = e;LENGTH = l;rect = new Rect[WIDTH*LENGTH];for(int i=0; i<WIDTH*LENGTH; i++){rect[i].map_x = i%WIDTH;rect[i].map_y = i/WIDTH;}rect[start].g_value = 0;rect[start].pre = NULL; }Astart::~Astart(void) { }//尋路算法核心 bool Astart::FindFastWay() {//關閉集合為空if(close_list.IsEmpty()){//出發點下邊的節點,此點必須在地圖內,出發點下邊還有路(不能是地圖邊界)if((start+WIDTH)/WIDTH < LENGTH && (start+WIDTH)/WIDTH >=0 && (start+WIDTH)%WIDTH >= 0 && (start+WIDTH)%WIDTH < WIDTH && start/WIDTH != LENGTH-1){//此點不是障礙物if( map[(start+WIDTH)/WIDTH][(start+WIDTH)%WIDTH]!=1 ){//修改該節點的各個值rect[start+WIDTH].pre = &rect[start];rect[start+WIDTH].h_value = get_h_value(start+WIDTH);rect[start+WIDTH].g_value = get_g_value(start+WIDTH);//插入到開放節點open_list.Insert(open_list.Length(),start+WIDTH);}}//出發點上邊的節點,此點必須在地圖內,出發點上邊還有路(不能是地圖邊界)if((start-WIDTH)/WIDTH < LENGTH && (start-WIDTH)/WIDTH >=0 && (start-WIDTH)%WIDTH >= 0 && (start-WIDTH)%WIDTH < WIDTH && (start/WIDTH) != 0){//此點不是障礙物if( map[(start-WIDTH)/WIDTH][(start-WIDTH)%WIDTH]!=1){//修改該節點的各個值rect[start-WIDTH].pre = &rect[start];rect[start-WIDTH].h_value = get_h_value(start-WIDTH);rect[start-WIDTH].g_value = get_g_value(start-WIDTH);//插入到開放節點open_list.Insert(open_list.Length(),start-WIDTH);}}//出發點右邊的節點,此點必須在地圖內,出發點右邊還有路(不能是地圖邊界)if((start+1)/WIDTH < LENGTH && (start+1)/WIDTH >=0 && (start+1)%WIDTH >= 0 && (start+1)%WIDTH < WIDTH && start!= start/WIDTH * WIDTH + WIDTH-1){//此點不是障礙物if( map[(start+1)/WIDTH][(start+1)%WIDTH]!=1){//修改該節點的各個值rect[start+1].pre = &rect[start];rect[start+1].h_value = get_h_value(start+1);rect[start+1].g_value = get_g_value(start+1);//插入到開放節點open_list.Insert(open_list.Length(),start+1);}}//出發點左邊的節點,此點必須在地圖內,出發點左邊還有路(不能是地圖邊界)if((start-1)/WIDTH < LENGTH && (start-1)/WIDTH >=0 && (start-1)%WIDTH >= 0 && (start-1)%WIDTH < WIDTH && start%WIDTH!=0){//此點不是障礙物if( map[(start-1)/WIDTH][(start-1)%WIDTH]!=1){//修改該節點的各個值rect[start-1].pre = &rect[start];rect[start-1].h_value = get_h_value(start-1);rect[start-1].g_value = get_g_value(start-1);//插入到開放節點open_list.Insert(open_list.Length(),start-1);}}//起始節點加入到關閉節點close_list.Insert(close_list.Length(),start);}////循環遍歷所有在開放列表的所有節點int i = 0; //最小評估值在開放列表中的標號int rectMinEstimateValue = -1; //最小評估值int stepRectID = 0; //下一步選擇的節點for(int z=1; z<=open_list.Length(); z++){int rectID; //節點代號int rectEstimateValue; //節點的評估值open_list.Find(z, rectID); //獲得開放列表中的節點代號rectEstimateValue = rect[rectID].h_value + rect[rectID].g_value;if(rectMinEstimateValue == -1)//第一個節點{rectMinEstimateValue = rectEstimateValue;}if(rectEstimateValue <= rectMinEstimateValue)//獲得更小節點{i = z;rectMinEstimateValue = rectEstimateValue;stepRectID = rectID;}}//沒有找到節點if(i==0) return false;//從開放列表刪除該節點,并插入到關閉列表里面int temp;open_list.Delete(i, temp);close_list.Insert(close_list.Length(),stepRectID);//最小評估值節點下邊的節點,此點必須在地圖內,該節點下邊還有路(不能是地圖邊界)if((stepRectID+WIDTH)/WIDTH < LENGTH && (stepRectID+WIDTH)/WIDTH >=0 && (stepRectID+WIDTH)%WIDTH >= 0 && (stepRectID+WIDTH)%WIDTH < WIDTH && (stepRectID/WIDTH) != LENGTH-1){//該節點是目標節點if(stepRectID+WIDTH == end){rect[stepRectID+WIDTH].pre = &rect[stepRectID];return true;}//該節點不是障礙物if( map[(stepRectID+WIDTH)/WIDTH][(stepRectID+WIDTH)%WIDTH]!=1 ){//該節點不在關閉列表里面if(close_list.Search(stepRectID+WIDTH) == 0){//該節點不在開放節點里面,就加入到開放節點里if(open_list.Search(stepRectID+WIDTH) == 0){//修改該節點的值rect[stepRectID+WIDTH].pre = &rect[stepRectID];rect[stepRectID+WIDTH].h_value = get_h_value(stepRectID+WIDTH);rect[stepRectID+WIDTH].g_value = get_g_value(stepRectID+WIDTH);//加入到開放節點open_list.Insert(open_list.Length(),stepRectID+WIDTH);}else{//如果該節點已存在開放列表里面,并且,通過剛剛一路走過來的路要比以前的路更近,則更新該節點的值if(rect[stepRectID].g_value + 10 < rect[stepRectID+WIDTH].g_value){//設置該節點的父節點rect[stepRectID+WIDTH].pre = &rect[stepRectID];//刷新實際到這里的值rect[stepRectID+WIDTH].g_value = get_g_value(stepRectID+WIDTH);}}}}}//最小評估值節點上邊的節點,此點必須在地圖內,該節點上邊還有路(不能是地圖邊界)if((stepRectID-WIDTH)/WIDTH < LENGTH && (stepRectID-WIDTH)/WIDTH >=0 && (stepRectID-WIDTH)%WIDTH >= 0 && (stepRectID-WIDTH)%WIDTH < WIDTH && (stepRectID/WIDTH) != 0){//該節點是目標節點if(stepRectID-WIDTH == end){rect[stepRectID-WIDTH].pre = &rect[stepRectID];return true;}//該節點不是障礙物if( map[(stepRectID-WIDTH)/WIDTH][(stepRectID-WIDTH)%WIDTH]!=1){//該節點不在關閉列表里面if(close_list.Search(stepRectID-WIDTH) == 0){//該節點不在開放節點里面,就加入到開放節點里if(open_list.Search(stepRectID-WIDTH) == 0){//修改該節點的值rect[stepRectID-WIDTH].pre = &rect[stepRectID];rect[stepRectID-WIDTH].h_value = get_h_value(stepRectID-WIDTH);rect[stepRectID-WIDTH].g_value = get_g_value(stepRectID-WIDTH);//加入到開放節點open_list.Insert(open_list.Length(),stepRectID-WIDTH);}else{//如果該節點已存在開放列表里面,并且,通過剛剛一路走過來的路要比以前的路更近,則更新該節點的值if(rect[stepRectID].g_value + 10 < rect[stepRectID-WIDTH].g_value){//設置該節點的父節點rect[stepRectID-WIDTH].pre = &rect[stepRectID];//刷新實際到這里的值rect[stepRectID-WIDTH].g_value = get_g_value(stepRectID-WIDTH);}}}}}//最小評估值節點右邊的節點,此點必須在地圖內,該節點右邊還有路(不能是地圖邊界)if((stepRectID+1)/WIDTH < LENGTH && (stepRectID+1)/WIDTH >=0 && (stepRectID+1)%WIDTH >= 0 && (stepRectID+1)%WIDTH < WIDTH && stepRectID!= stepRectID/WIDTH * WIDTH + WIDTH-1){//該節點是目標節點if(stepRectID+1 == end){rect[stepRectID+1].pre = &rect[stepRectID];return true;}//該節點不是障礙物if( map[(stepRectID+1)/WIDTH][(stepRectID+1)%WIDTH]!=1){//該節點不在關閉列表里面if(close_list.Search(stepRectID+1) == 0){//該節點不在開放節點里面,就加入到開放節點里if(open_list.Search(stepRectID+1) == 0){//修改該節點的值rect[stepRectID+1].pre = &rect[stepRectID];rect[stepRectID+1].h_value = get_h_value(stepRectID+1);rect[stepRectID+1].g_value = get_g_value(stepRectID+1);//加入到開放節點open_list.Insert(open_list.Length(),stepRectID+1);}else{//如果該節點已存在開放列表里面,并且,通過剛剛一路走過來的路要比以前的路更近,則更新該節點的值if(rect[stepRectID].g_value + 10 < rect[stepRectID+1].g_value){//設置該節點的父節點rect[stepRectID+1].pre = &rect[stepRectID];//刷新實際到這里的值rect[stepRectID+1].g_value = get_g_value(stepRectID+1);}}}}}//最小評估值節點左邊的節點,此點必須在地圖內,該節點左邊還有路(不能是地圖邊界)if((stepRectID-1)/WIDTH < LENGTH && (stepRectID-1)/WIDTH >=0 && (stepRectID-1)%WIDTH >= 0 && (stepRectID-1)%WIDTH < WIDTH && stepRectID%WIDTH!=0){//該節點是目標節點if(stepRectID-1 == end){rect[stepRectID-1].pre = &rect[stepRectID];return true;}//該節點不是障礙物if( map[(stepRectID-1)/WIDTH][(stepRectID-1)%WIDTH]!=1){//該節點不在關閉列表里面if(close_list.Search(stepRectID-1) == 0){//該節點不在開放節點里面,就加入到開放節點里if(open_list.Search(stepRectID-1) == 0){//修改該節點的值rect[stepRectID-1].pre = &rect[stepRectID];rect[stepRectID-1].h_value = get_h_value(stepRectID-1);rect[stepRectID-1].g_value = get_g_value(stepRectID-1);//加入到開放節點open_list.Insert(open_list.Length(),stepRectID-1);}else{//如果該節點已存在開放列表里面,并且,通過剛剛一路走過來的路要比以前的路更近,則更新該節點的值if(rect[stepRectID].g_value + 10 < rect[stepRectID-1].g_value){//設置該節點的父節點rect[stepRectID-1].pre = &rect[stepRectID];//刷新實際到這里的值rect[stepRectID-1].g_value = get_g_value(stepRectID-1);}}}}}//進行遞歸return FindFastWay(); }//距離目標的評估值 int Astart::get_h_value(int i) {return ((abs(end/WIDTH - i/WIDTH) + abs(end%WIDTH - i%WIDTH)) * 10); }//距離出發源的實際值 int Astart::get_g_value(int i) {return (rect[i].pre->g_value + 10); }//獲取路線結果 void Astart::GetResult() {Rect *p = &rect[end];while(p != NULL){value_list.Insert(value_list.Length(),*p);p = p->pre;} }#include "List.h" #pragma once class Rect { public:int map_x;int map_y;int h_value;int g_value;Rect *pre; };class Astart { public:Astart(int a[37][64],int w,int l,int s,int e);~Astart(void);bool Find();int get_h_value(int i);int get_g_value(int i);void GetResult();int map[37][64];Rect *rect;List<Rect> value_list; private:int WIDTH;int LENGTH;int start;int end;List<int> open_list;List<int> close_list; };
#include "list.h" template <class Type> List<Type>::~List() {length = 0;delete pHead;pHead = NULL; } template <class Type> List<Type>::List() {length = 0;pHead = NULL; } template <class Type> void List<Type>::Insert(int k, const Type & intnum) {if(k==0){if(length == 0){pHead = new Node<Type>;pHead->contents = intnum;length++;}else{Node<Type> *p= new Node<Type>;p->contents = intnum;p->next=pHead;pHead=p;length++;}}else if(k==length){Node<Type> *temp = pHead;for (int i=0;i<k-1;i++){temp = temp->next;}Node<Type> *newNode = new Node<Type>;newNode->contents = intnum;newNode->next = NULL;temp->next = newNode;length++;}else{Node<Type> *temp = pHead;for (int i=0;i<k-1;i++){temp = temp->next;}Node<Type> *newNode = new Node<Type>;newNode->contents = intnum;newNode->next = temp->next;temp->next = newNode;length++;} }template <class Type> void List<Type>::ShowListNode() {if(length == 0){cout<<"空表";}else{ Node<Type> *temp = pHead;for(int i=0;i<length;i++){cout<<"節點"<<i+1<<": "<<temp->contents<<endl;if(i!=length-1){temp = temp->next;}}} }template <class Type> void List<Type>::Delete(int k, Type & intnum) {Node<Type> *temp = pHead;if(k==1 && k==length){delete pHead;pHead = NULL;}else if(k==1){pHead = pHead->next;delete temp;}else if(k==length){for(int i=0; i<length-2; i++){temp = temp->next;}delete temp->next;temp->next = NULL;}else{for (int i=0; i<k-1; i++){temp = temp->next;}intnum = temp->contents;for(int i=k; i<length;i++){temp->contents = temp->next->contents;if(i != length-1){temp = temp->next;}else{delete temp->next;temp->next = NULL;break;}}}length--; } template <class Type> bool List<Type>::Find(int k, Type & intnum) const {if(k > length)return false;else{Node<Type> *temp = pHead ;for (int i=0; i<k-1; i++){temp = temp->next;}intnum = temp->contents;return true;} } template <class Type> int List<Type>::Search(const Type & intnum) const {Node<Type> *temp = pHead;for (int i=0; i<length; i++){if(temp->contents == intnum){return i+1;}if(i != length-1)temp = temp->next;}return 0; } template <class Type> void List<Type>::Modify(int k,const Type &intnum) {Node<Type> *temp = pHead ;for (int i=0; i<k-1; i++){temp = temp->next;}temp->contents = intnum; }
#pragma once #include "list.h" #include <iostream> using namespace std; template <class Type> class List; template <class Type> class Node {Type contents;Node * next;friend List<Type>; }; template <class Type> class List { public:List();~List();bool IsEmpty() const {return (pHead==NULL) ? true : false;}//判斷鏈表是否為空int Length() const {return length;} //返回鏈表中的元素總數bool Find(int k, Type & intnum) const; //尋找鏈表中的第k個元素,并將其傳送至intnumint Search(const Type & intnum) const; //尋找intnum,如果發現intnum,則返回intnum的位置,如果intnum不在鏈表中,則返回0void Delete(int k, Type & intnum); //把第k個元素取至intnum,然后從鏈表中刪除第k個元素void Insert(int k, const Type &intnum); //在第k個元素之后插入intnumvoid Modify(int k,const Type &intum);//將第k個元素的值修改為intnumvoid ShowListNode(); //顯示輸出鏈表的所有數據 private:Node<Type> *pHead;int length; };
// tt3.cpp : 定義應用程序的入口點。 //#include "stdafx.h" #include "tt3.h" #include <stdio.h> #include <fstream> #include "Astart.h" #include "List.h" #include "List.cpp" using namespace std;#define MAX_LOADSTRING 100// 全局變量: HINSTANCE hInst; // 當前實例 TCHAR szTitle[MAX_LOADSTRING]; // 標題欄文本 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口類名// 此代碼模塊中包含的函數的前向聲明: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);/// HDC hdc; //設備描述 HWND hWnd; //窗口句柄 HPEN pen,bluepen,greenpen,redpen; //畫筆 HBRUSH bluebrush,greenbrush,redbrush; //畫刷 int map[37][64]; //地圖數組 bool start = false,over = false; //開始結束是否被標記 int begin = 0,end = 0; //開始結束位置#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) /// //讀取上次地圖 void ReadMap() {ifstream in("try.map");for(int i=0;i<37;i++){for(int j=0;j<64;j++){in>>map[i][j];}}in.close(); }//刷新 void Update() {SelectObject(hdc,pen);for(int i=0;i<=37*20;i+=20){MoveToEx(hdc,0,i,NULL);LineTo(hdc,1280,i);}for(int i=0;i<=1280;i+=20){if(i==1280){MoveToEx(hdc,i-1,0,NULL);LineTo(hdc,i-1,740);}else{MoveToEx(hdc,i,0,NULL);LineTo(hdc,i,740);}}for(int i=0;i<37;i++){for(int j=0;j<64;j++){int x = j*20;int y = i*20;if(map[i][j] == 1){SelectObject(hdc,bluepen);SelectObject(hdc,bluebrush);if(x == 63*20){Rectangle(hdc,x+1,y+1,x+19,y+20);}else{Rectangle(hdc,x+1,y+1,x+20,y+20);}}else if(map[i][j] == 2){SelectObject(hdc,greenpen);SelectObject(hdc,greenbrush);if(x == 63*20){Rectangle(hdc,x+1,y+1,x+19,y+20);}else{Rectangle(hdc,x+1,y+1,x+20,y+20);}start = true;begin = i*64 + j;}else if(map[i][j] == 3){SelectObject(hdc,redpen);SelectObject(hdc,redbrush);if(x == 63*20){Rectangle(hdc,x+1,y+1,x+19,y+20);}else{Rectangle(hdc,x+1,y+1,x+20,y+20);}over = true;end = i*64 + j;}}} }int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow) {UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine);// TODO: 在此放置代碼。MSG msg;HACCEL hAccelTable;// 初始化全局字符串LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);LoadString(hInstance, IDC_TT3, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance);// 執行應用程序初始化:if (!InitInstance (hInstance, nCmdShow)){return FALSE;}hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TT3));/////加載地圖for(int i=0;i<37;i++){for(int j=0;j<64;j++){map[i][j] = 0;}}ReadMap();//背景繪制hdc = GetDC(hWnd);pen = ::CreatePen(PS_SOLID,1,RGB(255,255,255));bluepen = ::CreatePen(PS_SOLID,1,RGB(0,0,255));greenpen = ::CreatePen(PS_SOLID,1,RGB(0,255,0));redpen = ::CreatePen(PS_SOLID,1,RGB(255,0,0));bluebrush = ::CreateSolidBrush(RGB(0,0,255));greenbrush = ::CreateSolidBrush(RGB(0,255,0));redbrush = ::CreateSolidBrush(RGB(255,0,0));SelectObject(hdc,pen);for(int i=0;i<=37*20;i+=20){MoveToEx(hdc,0,i,NULL);LineTo(hdc,1280,i);}for(int i=0;i<=1280;i+=20){if(i==1280){MoveToEx(hdc,i-1,0,NULL);LineTo(hdc,i-1,740);}else{MoveToEx(hdc,i,0,NULL);LineTo(hdc,i,740);}}//刷新界面Update();// 主消息循環: // while (GetMessage(&msg, NULL, 0, 0)) // { // if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) // { // TranslateMessage(&msg); // DispatchMessage(&msg); // } // }while (true) {if(KEYDOWN(VK_ESCAPE)){SendMessage(hWnd,WM_DESTROY,0,0);}if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) ){if(msg.message==WM_QUIT)break;TranslateMessage( &msg );DispatchMessage( &msg );}}return (int) msg.wParam; }// // 函數: MyRegisterClass() // // 目的: 注冊窗口類。 // // 注釋: // // 僅當希望 // 此代碼與添加到 Windows 95 中的“RegisterClassEx” // 函數之前的 Win32 系統兼容時,才需要此函數及其用法。調用此函數十分重要, // 這樣應用程序就可以獲得關聯的 // “格式正確的”小圖標。 // ATOM MyRegisterClass(HINSTANCE hInstance) {WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = LoadIcon(NULL, IDI_INFORMATION);wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TT3);wcex.lpszClassName = "lujian";wcex.hIconSm = LoadIcon(wcex.hInstance, NULL);return RegisterClassEx(&wcex); }// // 函數: InitInstance(HINSTANCE, int) // // 目的: 保存實例句柄并創建主窗口 // // 注釋: // // 在此函數中,我們在全局變量中保存實例句柄并 // 創建和顯示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {hInst = hInstance; // 將實例句柄存儲在全局變量中hWnd = CreateWindow( "LuJian","windows",WS_POPUP,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);if (!hWnd){return FALSE;}ShowWindow(hWnd, SW_MAXIMIZE);UpdateWindow(hWnd);return TRUE; }// // 函數: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目的: 處理主窗口的消息。 // // WM_COMMAND - 處理應用程序菜單 // WM_PAINT - 繪制主窗口 // WM_DESTROY - 發送退出消息并返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {int wmId, wmEvent;PAINTSTRUCT ps;switch (message){case WM_COMMAND:wmId = LOWORD(wParam);wmEvent = HIWORD(wParam);// 分析菜單選擇:switch (wmId){case IDM_ABOUT:DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);break;case IDM_EXIT:DestroyWindow(hWnd);break;case ID_SAVE:{ofstream out("try.map");for(int i=0; i<37; i++){for(int j=0; j<64; j++){out<<map[i][j]<<" ";}out<<endl;}out.close();}break;case ID_LOAD:{ReadMap();Update();}break;case ID_RESET:{HBRUSH backbrush = (HBRUSH)GetStockObject(BLACK_BRUSH);SelectObject(hdc,backbrush);RECT rect;GetClientRect(hWnd,&rect);Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);SelectObject(hdc,pen);for(int i=0;i<=37*20;i+=20){MoveToEx(hdc,0,i,NULL);LineTo(hdc,1280,i);}for(int i=0;i<=1280;i+=20){if(i==1280){MoveToEx(hdc,i-1,0,NULL);LineTo(hdc,i-1,740);}else{MoveToEx(hdc,i,0,NULL);LineTo(hdc,i,740);}}for(int i=0;i<37;i++){for(int j=0;j<64;j++){map[i][j] = 0;}}start = false;}break;case ID_START:{SetBkColor(hdc,RGB(0,0,0));SetTextColor(hdc,RGB(255,255,255));TextOut(hdc,400,745," ",(int)strlen(" "));Astart astart(map,64,37,begin,end);if(!astart.Find()){SetBkColor(hdc,RGB(0,0,0));SetTextColor(hdc,RGB(255,255,255));TextOut(hdc,400,745,"沒有可以到達的路線",(int)strlen("沒有可以到達的路線"));break;}astart.GetResult();for(int i=astart.value_list.Length(); i>=1; i--){DWORD w1 = GetTickCount();Rect a;astart.value_list.Find(i,a);int x = a.map_x *20;int y = a.map_y *20;SelectObject(hdc,redpen);SelectObject(hdc,redbrush);if(x == 63*20){Rectangle(hdc,x+1,y+1,x+19,y+20);}else{Rectangle(hdc,x+1,y+1,x+20,y+20);}while(GetTickCount() - w1 <= 10);}}break;default:return DefWindowProc(hWnd, message, wParam, lParam);}break;case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// TODO: 在此添加任意繪圖代碼...Update();EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;case WM_MOUSEMOVE:{char buffer[13] = {};sprintf(buffer,"X=%d,Y=%d",LOWORD(lParam),HIWORD(lParam));SetBkColor(hdc,RGB(0,0,0));SetTextColor(hdc,RGB(255,255,255));TextOut(hdc,10,745,buffer,(int)strlen(buffer));}break;case WM_LBUTTONDOWN:{char buffer[15] = {};int x = LOWORD(lParam);int y = HIWORD(lParam);x=x/20*20;y=y/20*20;if(map[y/20][x/20] == 1){map[y/20][x/20] = 0;SelectObject(hdc,pen);SelectObject(hdc,GetStockObject(BLACK_BRUSH));Rectangle(hdc,x,y,x+21,y+21);}else if(map[y/20][x/20] == 2){start = false;map[y/20][x/20]=1;SelectObject(hdc,bluepen);SelectObject(hdc,bluebrush);Rectangle(hdc,x+1,y+1,x+20,y+20);}else{map[y/20][x/20]=1;SelectObject(hdc,bluepen);SelectObject(hdc,bluebrush);Rectangle(hdc,x+1,y+1,x+20,y+20);}sprintf(buffer,"X=%d,Y=%d",LOWORD(lParam),HIWORD(lParam));SetBkColor(hdc,RGB(0,0,0));SetTextColor(hdc,RGB(255,255,255));TextOut(hdc,10,745,buffer,(int)strlen(buffer));}break;case WM_RBUTTONDOWN:{char buffer[15] = {};int x = LOWORD(lParam);int y = HIWORD(lParam);x=x/20*20;y=y/20*20;if(!start){start = true;SelectObject(hdc,greenpen);SelectObject(hdc,greenbrush);Rectangle(hdc,x+1,y+1,x+20,y+20);sprintf(buffer,"X=%d,Y=%d",LOWORD(lParam),HIWORD(lParam));SetBkColor(hdc,RGB(0,0,0));SetTextColor(hdc,RGB(255,255,255));TextOut(hdc,10,745,buffer,(int)strlen(buffer));map[y/20][x/20]=2;begin = y/20*64 + x/20;}else{if(!over){over = true;SelectObject(hdc,redpen);SelectObject(hdc,redbrush);Rectangle(hdc,x+1,y+1,x+20,y+20);sprintf(buffer,"X=%d,Y=%d",LOWORD(lParam),HIWORD(lParam));SetBkColor(hdc,RGB(0,0,0));SetTextColor(hdc,RGB(255,255,255));TextOut(hdc,10,745,buffer,(int)strlen(buffer));map[y/20][x/20]=3;end = y/20*64 + x/20;} // else // { // map[end/64][end%64] = 0; // SelectObject(hdc,pen); // SelectObject(hdc,GetStockObject(BLACK_BRUSH)); // Rectangle(hdc,(end%64)*20,(end/64)*20,(end%64*20)+21,(end/64*20)+21); // over = false; // }}}default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0; }// “關于”框的消息處理程序。 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {UNREFERENCED_PARAMETER(lParam);switch (message){case WM_INITDIALOG:return (INT_PTR)TRUE;case WM_COMMAND:if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL){EndDialog(hDlg, LOWORD(wParam));return (INT_PTR)TRUE;}break;}return (INT_PTR)FALSE; }
上面就是主要源碼,但還不是全部工程,還有些資源文件。如有需要,請下載整個工程
http://download.csdn.net/detail/liujiayu2/8910871
總結
以上是生活随笔為你收集整理的A星算法(VC版源码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个很好地List实现源码
- 下一篇: 【玩转cocos2d-x之六】节点类CC