小编程题之A-Star算法的应用

来源:互联网 发布:2345软件大全官方网站 编辑:程序博客网 时间:2024/05/15 23:40

题目,就直接给几个截图吧。


具体要求


--------------------------------------------------------------------------------------------------------------------------------------------------------

先上传源代码和运行结果,有时间再分析一下:

orienteering.cpp

#include"a_star.h"class Orienteering {public:void main();};void Orienteering::main() {// TODO: Implement this function//char arry[][];int i,j;int movetime;Point s_tmp;Point g_tmp;    freopen("a.txt", "r", stdin);cin>>mapRows>>mapCols;cout<<"mapRows is "<<mapRows<<"and mapCols is "<<mapCols<<endl;for(i=0;i<mapRows;i++){string s;cin>>s;arry.push_back(s);arry_simple.push_back(s);}do_arrysimple();cout<<"show map"<<endl<<endl;for(i=0;i<mapRows;i++){cout<<arry[i]<<endl;}cout<<endl<<endl;cout<<"show map_simple"<<endl<<endl;for(i=0;i<mapRows;i++){cout<<arry_simple[i]<<endl;}cout<<endl<<endl;seek_function();s_tmp.x=start.x;s_tmp.y=start.y;g_tmp.x=at[0].x;g_tmp.y=at[0].y;movetime=0;cout<<"we find the route"<<endl;    for(i=0;i<1+at.size();i++){  a_starRoute(s_tmp, g_tmp,arry_simple, route);  for(j=0;j<route.size()-1;j++){cout<<route[j].x<<" "<<route[j].y<<endl;movetime++;}  s_tmp.x=at[i].x;  s_tmp.y=at[i].y;  if(i==at.size()-1)  {g_tmp.x=goal.x;g_tmp.y=goal.y;  }  else  {    g_tmp.x=at[i+1].x;g_tmp.y=at[i+1].y;  }}    cout<<goal.x<<" "<<goal.y<<endl;    cout<<"we find movetime is "<<movetime<<endl;}int main(int argc, char* argv[]) {Orienteering o;o.main();return 0;}

a_star.cpp

#include"a_star.h"using namespace std;    Point start;Point goal;vector<string> arry;vector<string> arry_simple;vector<Point>  route;vector<Point>  pass_a;int dir[4][2] = {{0, 1} ,{0, -1}, {-1, 0}, {1, 0}};unsigned int mapRows;unsigned int mapCols;void do_arrysimple(){int i,j;for(i=0;i<arry_simple.size();i++){for(j=0;j<arry_simple.size();j++){if(arry_simple[i][j]=='s')arry_simple[i][j]='-';if(arry_simple[i][j]=='g')arry_simple[i][j]='-';if(arry_simple[i][j]=='@')arry_simple[i][j]='-';}}}vector <Point> at;void seek_function(){    int min=0;Point p;Point temp;Point start_temp;int i,j;vector <Point> at_temp;for(i=0;i<mapRows;i++){for(j=0;j<mapCols;j++){if(arry[i][j]=='s'){start.x=i;start.y=j;}else if(arry[i][j]=='g'){goal.x=i;goal.y=j;}else if(arry[i][j]=='@'){p.x=i;p.y=j;p.g=0;at_temp.push_back(p);}}}cout<<"find s and g"<<endl;cout<<start.x<<start.y<<"    "<<goal.x<<goal.y<<endl;cout<<"find @"<<endl;for(i=0;i<at_temp.size();i++){cout<<at_temp[i].x<<" "<<at_temp[i].y<<endl;}    start_temp.x=start.x;start_temp.y=start.y;for(i=0;i<at_temp.size();i++){min=25;for(j=0;j<at_temp.size();j++){if((min>abs(start_temp.x-at_temp[j].x)+abs(start_temp.y-at_temp[j].y))&&at_temp[j].g==0){min = abs(start_temp.x-at_temp[j].x)+abs(start_temp.y-at_temp[j].y);temp.x=at_temp[j].x;temp.y=at_temp[j].y;temp.g=j;}}        at.push_back(temp);at_temp[temp.g].g=1;start_temp.x=temp.x;    start_temp.y=temp.y;}cout<<"find nearest @"<<endl;for(i=0;i<at.size();i++){cout<<at[i].x<<at[i].y<<endl;}}int getH(Point p, Point endp){return abs(endp.x - p.x)+ abs(endp.y - p.y);}static bool checkWorkerRouteAvai(Point p, vector<string> map){if (p.x+1 == 0)return false;if (p.y+1 == 0)return false;if (p.x >= mapRows)return false;if (p.y >= mapCols)return false;if ((map[p.x][p.y] != '-'))return false;return true;}unsigned int a_starRoute(Point start, Point goal,vector<string> map, vector<Point> &route){vector<Point*> closedset;vector<Point*> openset;Point *pStartPoint = new Point;Point endp;endp.x = goal.x;endp.y = goal.y;pStartPoint->x = start.x;pStartPoint->y = start.y;if (start.x == goal.x && start.y == goal.y)return A_STAR_GOAL;pStartPoint->g = 0;pStartPoint->f = pStartPoint->g + getH(*pStartPoint,endp);pStartPoint->pre = NULL;openset.push_back(pStartPoint);while (openset.size() != 0){int min = openset[0]->f;int min_f_idx = 0;for (int i=0; i<openset.size(); i++){if (openset[i]->f < min){min = openset[i]->f;min_f_idx = i;}}Point* pCurrent = openset[min_f_idx];if (pCurrent->x == endp.x && pCurrent->y == endp.y){Point *p = pCurrent;route.resize(0);while (p != NULL){route.push_back(*p);p = p->pre;}reverse(route.begin(), route.end());for (int j=0; j<openset.size(); j++){delete openset[j];}for (int j=0; j<closedset.size(); j++){delete closedset[j];}return A_STAR_RCHABLE;}closedset.push_back(openset[min_f_idx]);openset.erase(openset.begin() + min_f_idx);for (int i=0; i<4; i++){Point* pNb = new Point;pNb->x = pCurrent->x + dir[i][0];pNb->y = pCurrent->y + dir[i][1];bool inClosedset = false;for (int j=0; j<closedset.size(); j++){if (closedset[j]->x == pNb->x && closedset[j]->y == pNb->y){inClosedset = true;break;}}if (inClosedset){delete pNb;continue;}if (!checkWorkerRouteAvai(*pNb, map)){delete pNb;continue;}int tentative_g_score = pCurrent->g + 1;bool inOpenset = false;int inOpensetIdx = 0;for (int j=0; j<openset.size(); j++){if (openset[j]->x == pNb->x && openset[j]->y == pNb->y){delete pNb;inOpenset = true;inOpensetIdx = j;break;}}if (!inOpenset){pNb->pre = pCurrent;pNb->g = tentative_g_score;pNb->f = pNb->g + getH(*pNb, endp);openset.push_back(pNb);}else if (openset[inOpensetIdx]->g > tentative_g_score){    openset[inOpensetIdx]->pre = pCurrent;openset[inOpensetIdx]->g = tentative_g_score;openset[inOpensetIdx]->f = openset[inOpensetIdx]->g + getH(*openset[inOpensetIdx], endp);}}}for (int j=0; j<openset.size(); j++){delete openset[j];}for (int j=0; j<closedset.size(); j++){delete closedset[j];}return A_STAR_UNRCHABLE;}

a_star.h

#ifndef _A_START_H_#define _A_START_N_#include<vector>#include<string>#include<iostream>#include<math.h>#include<stdlib.h>#include<algorithm>using namespace std;#define A_STAR_RCHABLE1#define A_STAR_GOAL2#define A_STAR_UNRCHABLE3#define A_STAR_MOVE_FAILED4struct Point{int x;int y;int g;int f;Point* pre;};extern unsigned int mapRows;extern unsigned int mapCols;extern   Point start;extern Point goal;externvector<string> arry;extern  vector<string> arry_simple;externvector<Point>  route;externvector<Point>  pass_a;extern  vector <Point> at;extern unsigned int a_starRoute(Point start, Point goal,vector<string> map, vector<Point> &route);extern void seek_function();extern void do_arrysimple();#endif

地图输入:

6 6#######s@@####@-###-@-###-#-g#######

运行测试结果:g++ -std=c++11 -O2 -o a.out orienteering.cpp  a_star.cpp

mapRows is 6and mapCols is 6show map#######s@@####@-###-@-###-#-g#######show map_simple#######---####--###---###-#--#######find s and g11    44find @1 21 32 23 2find nearest @12132232we find the route1 11 21 31 22 23 23 34 34 4we find movetime is 8
(待续)
--------------------------------------------------------------------------------------------------------------------------------------------------------------


0 0
原创粉丝点击