一颗c++星,怎么了
A star c++, what is wrong
就在这里,我不知道为什么它不起作用。有时还好,有时崩溃,有时返回错误的方式。。。
//apath.h
#ifndef APATH_H
#define APATH_H
#include <vector>
#include <cmath>
#include <memory>
#include <allegro.h>
using namespace std;
double len(int x1, int y1, int x2, int y2);
class GameMap;
class point
{
public:
int x,y;
double f,g;
point *parent;
point(int _x=0, int _y=0, point *par=NULL) {x=_x; y=_y; parent=par; f=0; g=0;};
void countF(int sx, int sy, int tx, int ty)
{
g=len(x,y,tx,ty);
f=g+len(sx,sy,x,y);
};
};
struct point2d
{
int x,y;
};
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const point2d directions[]=
{
{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1},
};
class path
{
public:
vector<point2d> way;
int step;
bool findPath(int x, int y, int fx, int fy, GameMap& map, BITMAP* out);
private:
vector<point> open;
vector<point> closed;
};
#endif
和apath.cpp:
//apath.cpp
#include "apath.h"
#include <vector>
#include <cmath>
double len(int x1, int y1, int x2, int y2)
{
return (sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
//return max(abs(x1-x2), abs(y1-y2));
/*int mx=max(x1,x2)-min(x1,x2);
int my=max(y1,y2)-min(y1,y2);
return (min(mx,my)*14+max(mx,my)-min(mx,my)*10);*/
};
bool findPoint(vector<point> arr, point pt, point &ret)
{
for (int i=0; i<arr.size(); i++) if (arr[i].x==pt.x && arr[i].y==pt.y) { ret=arr[i]; return true;}
return ' ';
};
//GameMap持有地图。。。
bool path::findPath(int x, int y, int fx, int fy, GameMap& map, BITMAP* out)
{
while(!open.empty()) open.pop_back();
while(!closed.empty()) closed.pop_back();
while(!way.empty()) way.pop_back();
point start(x,y);
start.countF(x,y,fx,fy);
open.push_back(start);
point finish(fx,fy);
double min=999999999;
int index;
point tmp;
point *tmp2;
point comparer;
while (!findPoint(closed,finish,comparer))
{
min=999999999;
for (int i=0; i<open.size(); i++)
{
if(open[i].f<min)
{
min=open[i].f;
index=i;
}
}
tmp=open[index];
closed.push_back(open[index]);
open.erase(open.begin()+index);
for (int i=0; i<8; i++)
{
tmp2=new point(tmp.x+directions[i].x,tmp.y+directions[i].y);
if (map.getCollisionXY(tmp2->x,tmp2->y)==1 // map.getCollision returns 1 when you cant pass through the tile and 0 otherwise...
|| findPoint(closed,*tmp2,comparer)) continue;
if (!findPoint(open,*tmp2,comparer))
{
point newP(tmp.x+directions[i].x,tmp.y+directions[i].y,&closed[closed.size()-1]);
newP.countF(x,y,fx,fy);
open.push_back(newP);
}
else
{
if (comparer.g>tmp.g)
{
comparer.parent=&closed[closed.size()-1];
comparer.countF(x,y,fx,fy);
}
}
delete tmp2;
}
/* for (int i=0; i<open.size(); i++)
if (open[i].parent!=0)circlefill(out,open[i].x*16+4,open[i].y*16+4,3,0xff0000);
if (open.empty()) return false;
for (int i=0; i<closed.size(); i++)
if (closed[i].parent!=0)circlefill(out,closed[i].x*16+12,closed[i].y*16+12,3,0xffff00);*/ //debug draw
}
point2d pt;
pt.x=finish.x;
pt.y=finish.y;
way.push_back(pt);
point wayer;
wayer=closed[closed.size()-1];
while(wayer.parent!=0) //CRASH from here
{
wayer=*wayer.parent;
pt.x=wayer.x;
pt.y=wayer.y;
way.push_back(pt);
// circlefill(out,pt.x*16+12,pt.y*16+12,3,0xffffff); //debug draw
} //CRASH to here, i dont exacly know where it is, but when trying to recreate path.
return true;
};
一些解释:GameMap是带地图的类并且它的getCollisionXY返回1,否则返回0。
谢谢你的帮助。
我看到的一个可能导致访问冲突的主要来源是,您的点包含作为指针的父级,并且它们是向量的成员。如果向量被重新分配,这些指针可能会失效。
除此之外,很难立即看出是什么原因导致了此次事故。
你真的应该通过调试器运行你的代码。我刚刚指出了一件事,我马上就明白了。
相关文章:
- 不断收到相同的错误...怎么了?C++
- 编译器没有显示错误,但我没有得到任何输出.怎么了?
- qsort 不适用于我的 impl 中的浮点类型。怎么了?
- 在C++中表现不佳.怎么了?
- 我的程序怎么了?(C )
- 这里怎么了?单位转换器
- 错误:未定义对 .. 的引用.怎么了
- 我试图编写一个代码来查找一个单词是否是回文,但它不起作用。怎么了?
- 我在C++文件中的简单代码块上有 2 个错误。错误 C2059:语法错误:"?"。第二智能感知:预期表达式。怎么了?
- 我的代码怎么了?带有QT的原始输入API
- 使用 cin.get() 似乎没有读取我期望的字符。怎么了?
- 当其中一个字符串来自cin时,无法比较两个字符串.这里怎么了
- "5"怎么了?纸上一切都很好
- 我的cout怎么了
- C++:腹肌怎么了
- Qt OpenGL with Widgets:怎么了?
- 程序为某些条目提供了错误的输出!怎么了?
- 自动和复制构造函数:怎么了?
- 怎么了???只有七行
- Qt简单的文本编辑器,怎么了?