m=m;this->straightCost=10;this->diagon" />
#include
#include
#include "Node.h"
#include "AStar.h"
#define pi 3.141592
using std::cout;
using std::endl;
using std::vector;
using std::vector::iterator;
AStar::AStar(Map m){//cout<<"11111"<
this->straightCost=10;
this->diagonalCost=14;
}
AStar::~AStar(){
//cout<<"destory aStar"<<"\n";
}
int AStar::judgeCanAccess(int row1,int col1,int row2,int col2){//cout<<"22222"<
return(-1);
}
if(row2<0||row2>=ROW||col2<0||col2>=COL){//位置不合法
return(-1);
}
if(this->m->map[row1][col1]==0||this->m->map[row2][col2]==0){//位置不合法
return(-1);
}
this->startNode.setRow(row1);
this->startNode.setCol(col1);
Link ptr_temp=(Link)malloc(sizeof(LinkNode));
ptr_temp->node=this->startNode;ptr_temp->parent=NULL;
this->openList.push_back(ptr_temp);//把开始节点放进开启列表
this->endNode.setRow(row2);
this->endNode.setCol(col2);
this->searchRoad();//寻找路径
if(this->roadList.size()==0){//没有路
return(0);
}
return(1);
}
vector AStar::returnRoadList(){
return(this->roadList);
}
vector AStar::returnRoadNodeList(){
float temp_minus=0.0;
Link ptr_temp=(Link)malloc(sizeof(LinkNode));
ptr_temp=this->roadList[0];
vector roadNodeList;
roadNodeList.push_back(ptr_temp);//把第一个节点放入
for(int i=1;i
temp_minus=atan2(this->roadList[i+1]->node.getCol()-ptr_temp->node.getCol(),this->roadList[i+1]->node.getRow()-ptr_temp->node.getRow())-
atan2(this->roadList[i]->node.getCol()-ptr_temp->node.getCol(),this->roadList[i]->node.getRow()-ptr_temp->node.getRow());
if(temp_minus>0.00001||temp_minus<-0.00001){
ptr_temp=this->roadList[i];
roadNodeList.push_back(ptr_temp);
}
}
roadNodeList.push_back(this->roadList[this->roadList.size()-1]);//加入最后一个节点
return(roadNodeList);
}
void AStar::searchRoad(){//cout<<"33333"<
iterator p;
Link ptr=(Link)malloc(sizeof(LinkNode));ptr->parent=NULL;
while(this->openList.size()!=0){
ptr=openList[0];
if((ptr->node.getRow()==this->endNode.getRow())&&(ptr->node.getCol()==this->endNode.getCol())){
isFind=1;
break;
}
if(ptr->node.getRow()>=1&&ptr->node.getCol()<=9){
if(ptr->node.getRow()-1>=0){//node上边的节点
findParent(ptr->node.getRow()-1,ptr->node.getCol(),ptr,this->straightCost);
}
if(ptr->node.getRow()+1
}
if(ptr->node.getCol()-1>=0){//node左边的节点
findParent(ptr->node.getRow(),ptr->node.getCol()-1,ptr,this->straightCost);
}
if(ptr->node.getCol()+1
}
}
else{
if(ptr->node.getCol()-1>=0){//node左边的节点
find
Parent(ptr->node.getRow(),ptr->node.getCol()-1,ptr,this->straightCost);
}
if(ptr->node.getCol()+1
e.getF()){
ptr_temp=openList[i];
index=i;
}
}
if(index!=0){
openList[index]=openList[0];
openList[0]=ptr_temp;
}
}