rt,我的A*是这样写的:
struct Node{
int x,y;
int F,G,H;
bool operator<(const Node &a)const{
return F==a.F?G>a.G:F>a.F;
}
};
int AStar_js(pair<int,int> start,pair<int,int> end){
int x=start.first,y=start.second;
int x1=end.first,y1=end.second;
return abs(x-x1)+abs(y-y1);
}
Node make_node(pair<int,int> now,int G,int H){
int F=G+H;
Node Temp;
Temp.x=now.first;
Temp.y=now.second;
Temp.F=F;
Temp.G=G;
Temp.H=H;
return Temp;
}
priority_queue<Node> NEW;
void AStar(Gird a[105][105],pair<int,int> start,pair<int,int> end,int n){
bool vis[n+2][n+2]={0};
int b[n+2][n+2]={0};
vector<pair<int,int> > f[105][105];
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
f[i][j].clear();
}
}
if(check(start,end)==false) return;
priority_queue<Node> pq=NEW;
pq.push(make_node(start,0,AStar_js(start,end)));
vis[start.first][start.second]=1;
b[start.first][start.second]=0;
while(pq.size()>0){
Node now=pq.top();
pq.pop();
if(make_pair(now.x,now.y)==end){
for(int i=0;i<f[end.first][end.second].size(); i++){
a[f[end.first][end.second][i].first][f[end.first][end.second][i].second].color=RGB(255,0,255);
Fill(a,n,n,825/n);
}
return;
}
int x=now.x,y=now.y;
priority_queue<Node> pq2=NEW;
for(int i=0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
if(vis[xx][yy]==0&&a[xx][yy].no==0){
int xx=pq2.top().x,yy=pq2.top().y;
b[xx][yy]=b[x][y]+1;
vis[xx][yy]=1;
f[xx][yy].push_back({x,y});
pq.push(make_node({xx,yy},b[xx][yy],AStar_js({xx,yy},end)));
a[xx][yy].color=BLUE;
a[xx][yy].data=b[xx][yy];
Fill(a,n,n,825/n);
break;
}
}
}
}
return;
}
但是有时候不是最短路径,甚至有时候直接就没了,怎么改呢?