A星算法(VC版源码)
生活随笔
收集整理的這篇文章主要介紹了
A星算法(VC版源码)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
源碼不是我寫的,如有侵權(quán),請(qǐng)聯(lián)系我,更改版權(quán)
使用方法:
對(duì)于空地左鍵單擊后會(huì)產(chǎn)生障礙,對(duì)障礙左鍵單擊會(huì)消除障礙,對(duì)于起點(diǎn),兩次左鍵盤單擊會(huì)消除起點(diǎn),如果不存在起點(diǎn),單擊右鍵會(huì)產(chǎn)生起點(diǎn),如果存在起點(diǎn)不存在終點(diǎn),單擊右鍵會(huì)產(chǎn)生終點(diǎn),如果既存在起點(diǎn)又存在終點(diǎn),單擊右鍵會(huì)消除終點(diǎn),點(diǎn)擊開始尋路回畫出路徑
效果圖:
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() {//關(guān)閉集合為空if(close_list.IsEmpty()){//出發(fā)點(diǎn)下邊的節(jié)點(diǎn),此點(diǎn)必須在地圖內(nèi),出發(fā)點(diǎn)下邊還有路(不能是地圖邊界)if((start+WIDTH)/WIDTH < LENGTH && (start+WIDTH)/WIDTH >=0 && (start+WIDTH)%WIDTH >= 0 && (start+WIDTH)%WIDTH < WIDTH && start/WIDTH != LENGTH-1){//此點(diǎn)不是障礙物if( map[(start+WIDTH)/WIDTH][(start+WIDTH)%WIDTH]!=1 ){//修改該節(jié)點(diǎn)的各個(gè)值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);//插入到開放節(jié)點(diǎn)open_list.Insert(open_list.Length(),start+WIDTH);}}//出發(fā)點(diǎn)上邊的節(jié)點(diǎn),此點(diǎn)必須在地圖內(nèi),出發(fā)點(diǎn)上邊還有路(不能是地圖邊界)if((start-WIDTH)/WIDTH < LENGTH && (start-WIDTH)/WIDTH >=0 && (start-WIDTH)%WIDTH >= 0 && (start-WIDTH)%WIDTH < WIDTH && (start/WIDTH) != 0){//此點(diǎn)不是障礙物if( map[(start-WIDTH)/WIDTH][(start-WIDTH)%WIDTH]!=1){//修改該節(jié)點(diǎn)的各個(gè)值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);//插入到開放節(jié)點(diǎn)open_list.Insert(open_list.Length(),start-WIDTH);}}//出發(fā)點(diǎn)右邊的節(jié)點(diǎn),此點(diǎn)必須在地圖內(nèi),出發(fā)點(diǎn)右邊還有路(不能是地圖邊界)if((start+1)/WIDTH < LENGTH && (start+1)/WIDTH >=0 && (start+1)%WIDTH >= 0 && (start+1)%WIDTH < WIDTH && start!= start/WIDTH * WIDTH + WIDTH-1){//此點(diǎn)不是障礙物if( map[(start+1)/WIDTH][(start+1)%WIDTH]!=1){//修改該節(jié)點(diǎn)的各個(gè)值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);//插入到開放節(jié)點(diǎn)open_list.Insert(open_list.Length(),start+1);}}//出發(fā)點(diǎn)左邊的節(jié)點(diǎn),此點(diǎn)必須在地圖內(nèi),出發(fā)點(diǎn)左邊還有路(不能是地圖邊界)if((start-1)/WIDTH < LENGTH && (start-1)/WIDTH >=0 && (start-1)%WIDTH >= 0 && (start-1)%WIDTH < WIDTH && start%WIDTH!=0){//此點(diǎn)不是障礙物if( map[(start-1)/WIDTH][(start-1)%WIDTH]!=1){//修改該節(jié)點(diǎn)的各個(gè)值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);//插入到開放節(jié)點(diǎn)open_list.Insert(open_list.Length(),start-1);}}//起始節(jié)點(diǎn)加入到關(guān)閉節(jié)點(diǎn)close_list.Insert(close_list.Length(),start);}////循環(huán)遍歷所有在開放列表的所有節(jié)點(diǎn)int i = 0; //最小評(píng)估值在開放列表中的標(biāo)號(hào)int rectMinEstimateValue = -1; //最小評(píng)估值int stepRectID = 0; //下一步選擇的節(jié)點(diǎn)for(int z=1; z<=open_list.Length(); z++){int rectID; //節(jié)點(diǎn)代號(hào)int rectEstimateValue; //節(jié)點(diǎn)的評(píng)估值open_list.Find(z, rectID); //獲得開放列表中的節(jié)點(diǎn)代號(hào)rectEstimateValue = rect[rectID].h_value + rect[rectID].g_value;if(rectMinEstimateValue == -1)//第一個(gè)節(jié)點(diǎn){rectMinEstimateValue = rectEstimateValue;}if(rectEstimateValue <= rectMinEstimateValue)//獲得更小節(jié)點(diǎn){i = z;rectMinEstimateValue = rectEstimateValue;stepRectID = rectID;}}//沒有找到節(jié)點(diǎn)if(i==0) return false;//從開放列表刪除該節(jié)點(diǎn),并插入到關(guān)閉列表里面int temp;open_list.Delete(i, temp);close_list.Insert(close_list.Length(),stepRectID);//最小評(píng)估值節(jié)點(diǎn)下邊的節(jié)點(diǎn),此點(diǎn)必須在地圖內(nèi),該節(jié)點(diǎn)下邊還有路(不能是地圖邊界)if((stepRectID+WIDTH)/WIDTH < LENGTH && (stepRectID+WIDTH)/WIDTH >=0 && (stepRectID+WIDTH)%WIDTH >= 0 && (stepRectID+WIDTH)%WIDTH < WIDTH && (stepRectID/WIDTH) != LENGTH-1){//該節(jié)點(diǎn)是目標(biāo)節(jié)點(diǎn)if(stepRectID+WIDTH == end){rect[stepRectID+WIDTH].pre = &rect[stepRectID];return true;}//該節(jié)點(diǎn)不是障礙物if( map[(stepRectID+WIDTH)/WIDTH][(stepRectID+WIDTH)%WIDTH]!=1 ){//該節(jié)點(diǎn)不在關(guān)閉列表里面if(close_list.Search(stepRectID+WIDTH) == 0){//該節(jié)點(diǎn)不在開放節(jié)點(diǎn)里面,就加入到開放節(jié)點(diǎn)里if(open_list.Search(stepRectID+WIDTH) == 0){//修改該節(jié)點(diǎn)的值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);//加入到開放節(jié)點(diǎn)open_list.Insert(open_list.Length(),stepRectID+WIDTH);}else{//如果該節(jié)點(diǎn)已存在開放列表里面,并且,通過剛剛一路走過來的路要比以前的路更近,則更新該節(jié)點(diǎn)的值if(rect[stepRectID].g_value + 10 < rect[stepRectID+WIDTH].g_value){//設(shè)置該節(jié)點(diǎn)的父節(jié)點(diǎn)rect[stepRectID+WIDTH].pre = &rect[stepRectID];//刷新實(shí)際到這里的值rect[stepRectID+WIDTH].g_value = get_g_value(stepRectID+WIDTH);}}}}}//最小評(píng)估值節(jié)點(diǎn)上邊的節(jié)點(diǎn),此點(diǎn)必須在地圖內(nèi),該節(jié)點(diǎn)上邊還有路(不能是地圖邊界)if((stepRectID-WIDTH)/WIDTH < LENGTH && (stepRectID-WIDTH)/WIDTH >=0 && (stepRectID-WIDTH)%WIDTH >= 0 && (stepRectID-WIDTH)%WIDTH < WIDTH && (stepRectID/WIDTH) != 0){//該節(jié)點(diǎn)是目標(biāo)節(jié)點(diǎn)if(stepRectID-WIDTH == end){rect[stepRectID-WIDTH].pre = &rect[stepRectID];return true;}//該節(jié)點(diǎn)不是障礙物if( map[(stepRectID-WIDTH)/WIDTH][(stepRectID-WIDTH)%WIDTH]!=1){//該節(jié)點(diǎn)不在關(guān)閉列表里面if(close_list.Search(stepRectID-WIDTH) == 0){//該節(jié)點(diǎn)不在開放節(jié)點(diǎn)里面,就加入到開放節(jié)點(diǎn)里if(open_list.Search(stepRectID-WIDTH) == 0){//修改該節(jié)點(diǎn)的值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);//加入到開放節(jié)點(diǎn)open_list.Insert(open_list.Length(),stepRectID-WIDTH);}else{//如果該節(jié)點(diǎn)已存在開放列表里面,并且,通過剛剛一路走過來的路要比以前的路更近,則更新該節(jié)點(diǎn)的值if(rect[stepRectID].g_value + 10 < rect[stepRectID-WIDTH].g_value){//設(shè)置該節(jié)點(diǎn)的父節(jié)點(diǎn)rect[stepRectID-WIDTH].pre = &rect[stepRectID];//刷新實(shí)際到這里的值rect[stepRectID-WIDTH].g_value = get_g_value(stepRectID-WIDTH);}}}}}//最小評(píng)估值節(jié)點(diǎn)右邊的節(jié)點(diǎn),此點(diǎn)必須在地圖內(nèi),該節(jié)點(diǎn)右邊還有路(不能是地圖邊界)if((stepRectID+1)/WIDTH < LENGTH && (stepRectID+1)/WIDTH >=0 && (stepRectID+1)%WIDTH >= 0 && (stepRectID+1)%WIDTH < WIDTH && stepRectID!= stepRectID/WIDTH * WIDTH + WIDTH-1){//該節(jié)點(diǎn)是目標(biāo)節(jié)點(diǎn)if(stepRectID+1 == end){rect[stepRectID+1].pre = &rect[stepRectID];return true;}//該節(jié)點(diǎn)不是障礙物if( map[(stepRectID+1)/WIDTH][(stepRectID+1)%WIDTH]!=1){//該節(jié)點(diǎn)不在關(guān)閉列表里面if(close_list.Search(stepRectID+1) == 0){//該節(jié)點(diǎn)不在開放節(jié)點(diǎn)里面,就加入到開放節(jié)點(diǎn)里if(open_list.Search(stepRectID+1) == 0){//修改該節(jié)點(diǎn)的值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);//加入到開放節(jié)點(diǎn)open_list.Insert(open_list.Length(),stepRectID+1);}else{//如果該節(jié)點(diǎn)已存在開放列表里面,并且,通過剛剛一路走過來的路要比以前的路更近,則更新該節(jié)點(diǎn)的值if(rect[stepRectID].g_value + 10 < rect[stepRectID+1].g_value){//設(shè)置該節(jié)點(diǎn)的父節(jié)點(diǎn)rect[stepRectID+1].pre = &rect[stepRectID];//刷新實(shí)際到這里的值rect[stepRectID+1].g_value = get_g_value(stepRectID+1);}}}}}//最小評(píng)估值節(jié)點(diǎn)左邊的節(jié)點(diǎn),此點(diǎn)必須在地圖內(nèi),該節(jié)點(diǎn)左邊還有路(不能是地圖邊界)if((stepRectID-1)/WIDTH < LENGTH && (stepRectID-1)/WIDTH >=0 && (stepRectID-1)%WIDTH >= 0 && (stepRectID-1)%WIDTH < WIDTH && stepRectID%WIDTH!=0){//該節(jié)點(diǎn)是目標(biāo)節(jié)點(diǎn)if(stepRectID-1 == end){rect[stepRectID-1].pre = &rect[stepRectID];return true;}//該節(jié)點(diǎn)不是障礙物if( map[(stepRectID-1)/WIDTH][(stepRectID-1)%WIDTH]!=1){//該節(jié)點(diǎn)不在關(guān)閉列表里面if(close_list.Search(stepRectID-1) == 0){//該節(jié)點(diǎn)不在開放節(jié)點(diǎn)里面,就加入到開放節(jié)點(diǎn)里if(open_list.Search(stepRectID-1) == 0){//修改該節(jié)點(diǎn)的值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);//加入到開放節(jié)點(diǎn)open_list.Insert(open_list.Length(),stepRectID-1);}else{//如果該節(jié)點(diǎn)已存在開放列表里面,并且,通過剛剛一路走過來的路要比以前的路更近,則更新該節(jié)點(diǎn)的值if(rect[stepRectID].g_value + 10 < rect[stepRectID-1].g_value){//設(shè)置該節(jié)點(diǎn)的父節(jié)點(diǎn)rect[stepRectID-1].pre = &rect[stepRectID];//刷新實(shí)際到這里的值rect[stepRectID-1].g_value = get_g_value(stepRectID-1);}}}}}//進(jìn)行遞歸return FindFastWay(); }//距離目標(biāo)的評(píng)估值 int Astart::get_h_value(int i) {return ((abs(end/WIDTH - i/WIDTH) + abs(end%WIDTH - i%WIDTH)) * 10); }//距離出發(fā)源的實(shí)際值 int Astart::get_g_value(int i) {return (rect[i].pre->g_value + 10); }//獲取路線結(jié)果 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<<"節(jié)點(diǎn)"<<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;} //返回鏈表中的元素總數(shù)bool Find(int k, Type & intnum) const; //尋找鏈表中的第k個(gè)元素,并將其傳送至intnumint Search(const Type & intnum) const; //尋找intnum,如果發(fā)現(xiàn)intnum,則返回intnum的位置,如果intnum不在鏈表中,則返回0void Delete(int k, Type & intnum); //把第k個(gè)元素取至intnum,然后從鏈表中刪除第k個(gè)元素void Insert(int k, const Type &intnum); //在第k個(gè)元素之后插入intnumvoid Modify(int k,const Type &intum);//將第k個(gè)元素的值修改為intnumvoid ShowListNode(); //顯示輸出鏈表的所有數(shù)據(jù) private:Node<Type> *pHead;int length; };
// tt3.cpp : 定義應(yīng)用程序的入口點(diǎn)。 //#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; // 當(dāng)前實(shí)例 TCHAR szTitle[MAX_LOADSTRING]; // 標(biāo)題欄文本 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口類名// 此代碼模塊中包含的函數(shù)的前向聲明: 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; //設(shè)備描述 HWND hWnd; //窗口句柄 HPEN pen,bluepen,greenpen,redpen; //畫筆 HBRUSH bluebrush,greenbrush,redbrush; //畫刷 int map[37][64]; //地圖數(shù)組 bool start = false,over = false; //開始結(jié)束是否被標(biāo)記 int begin = 0,end = 0; //開始結(jié)束位置#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);// 執(zhí)行應(yīng)用程序初始化: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();// 主消息循環(huán): // 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; }// // 函數(shù): MyRegisterClass() // // 目的: 注冊(cè)窗口類。 // // 注釋: // // 僅當(dāng)希望 // 此代碼與添加到 Windows 95 中的“RegisterClassEx” // 函數(shù)之前的 Win32 系統(tǒng)兼容時(shí),才需要此函數(shù)及其用法。調(diào)用此函數(shù)十分重要, // 這樣應(yīng)用程序就可以獲得關(guān)聯(lián)的 // “格式正確的”小圖標(biāo)。 // 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); }// // 函數(shù): InitInstance(HINSTANCE, int) // // 目的: 保存實(shí)例句柄并創(chuàng)建主窗口 // // 注釋: // // 在此函數(shù)中,我們?cè)谌肿兞恐斜4鎸?shí)例句柄并 // 創(chuàng)建和顯示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {hInst = hInstance; // 將實(shí)例句柄存儲(chǔ)在全局變量中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; }// // 函數(shù): WndProc(HWND, UINT, WPARAM, LPARAM) // // 目的: 處理主窗口的消息。 // // WM_COMMAND - 處理應(yīng)用程序菜單 // WM_PAINT - 繪制主窗口 // WM_DESTROY - 發(fā)送退出消息并返回 // // 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,"沒有可以到達(dá)的路線",(int)strlen("沒有可以到達(dá)的路線"));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; }// “關(guān)于”框的消息處理程序。 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; }
上面就是主要源碼,但還不是全部工程,還有些資源文件。如有需要,請(qǐng)下載整個(gè)工程
http://download.csdn.net/detail/liujiayu2/8910871
總結(jié)
以上是生活随笔為你收集整理的A星算法(VC版源码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个很好地List实现源码
- 下一篇: 【玩转cocos2d-x之六】节点类CC