紧急A*搜索迷宫C++; 问题是写一个A*搜索代码,以逃避迷宫的C++代码。
我有代码可以编译,但无法执行。当我在终端中运行它时,出现了一个错误“分段错误(堆芯转储)”。有人能帮我解决这个问题吗?谢谢 代码如下:紧急A*搜索迷宫C++; 问题是写一个A*搜索代码,以逃避迷宫的C++代码。,c++,search,maze,C++,Search,Maze,我有代码可以编译,但无法执行。当我在终端中运行它时,出现了一个错误“分段错误(堆芯转储)”。有人能帮我解决这个问题吗?谢谢 代码如下: #include <iostream> #include <algorithm> #include <cmath> #include <string> #include <vector> using namespace std; double heu(int a, int b) {
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
double heu(int a, int b)
{ return sqrt((a-11)*(a-11) + (b-0)*(b-0));}
class step{
public:
int x, y;
string path;
int pathcost() const {return path.length() - 1;}
double totalcost() const {return ((double) pathcost() + heu(x,y));}
bool comp(const step & , const step & ) ;
};
bool comp( const step & a, const step & b) {return (a.totalcost() < b.totalcost());}
void explore(step currentstep, char Maze[][12],vector<step> allsteps){
step tempstep;
if (( currentstep.x != 0 ) && (Maze[currentstep.x -1][currentstep.y] != '1'))
{tempstep.x = currentstep.x -1; tempstep.y = currentstep.y;
tempstep.path = currentstep.path + 'W';
allsteps.push_back(tempstep); }
if (( currentstep.x != 11 ) && (Maze[currentstep.x +1][currentstep.y] != '1'))
{ tempstep.x = currentstep.x +1; tempstep.y = currentstep.y;
tempstep.path = currentstep.path + 'E';
allsteps.push_back (tempstep);}
if (( currentstep.y != 0 ) && (Maze[currentstep.x][currentstep.y -1] != '1'))
{tempstep.x = currentstep.x; tempstep.y = currentstep.y -1;
tempstep.path = currentstep.path + 'N';
allsteps.push_back (tempstep);
}
if (( currentstep.y != 11 ) && (Maze[currentstep.x][currentstep.y +1] != '1'))
{tempstep.x = currentstep.x; tempstep.y = currentstep.y +1;
tempstep.path = currentstep.path + 'S';
allsteps.push_back (tempstep);}
}
int main()
{
string txt = "000100000000000100111111100100000000000000000000011001001101001001001000001000001000001000001000001111101011100000001000000111101000000100000000";
char Maze[12][12];
for (int i=0; i<12; i++){
for( int j =0; j<12;j++){
int k=i*12+j;
Maze[j][i]=txt[k];
}}
vector<step> allsteps;
step currentstep;
currentstep.x=0;currentstep.y=11;
currentstep.path="";
vector<step>::iterator itr;
while ((currentstep.x != 11) && (currentstep.y!=0)){
explore(currentstep, Maze,allsteps);
sort(allsteps.begin(), allsteps.end(),comp);
itr=allsteps.begin();
currentstep = *itr;}
cout<<currentstep.path;
system ("PAUSE");
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
双heu(整数a,整数b)
{返回sqrt((a-11)*(a-11)+(b-0)*(b-0));}
类步{
公众:
int x,y;
字符串路径;
int pathcost()常量{return path.length()-1;}
double totalcost()常量{return((double)pathcost()+heu(x,y));}
bool comp(常数步和常数步和);
};
bool comp(const step&a,const step&b){return(a.totalcost() 对于(inti=0;i您是否尝试过设置断点并调试代码
cout << "this is a test line, it will print if my program gets past this point." << endl;
cin.get();
cout使用gcc编译代码我得到以下信息:
#0 0x00000000004016e2 in step::operator= (this=0x7fffffffdb60) at main.cpp:13
#1 0x0000000000401468 in main () at main.cpp:73
如果我说这些话:
currentstep = *itr;}
及
也许它能帮你找到一些东西
检查向量的内容,并打印当前迭代
只是一张纸条
void explore(step currentstep, char Maze[][12],vector<step> allsteps)
allsteps为空,因此*itr
将是无效的迭代器,并将对代码进行分段。更改代码以通过引用而不是通过值传递所有步骤:
void explore(step currentstep, char Maze[][12],vector<step>& allsteps){
void explore(步骤currentstep,字符迷宫[][12],向量和所有步骤){
不是
void explore(步骤currentstep,字符迷宫[][12],向量所有步骤){
<代码>我使用DEV-C++,我认为我不能使用这个方法:(你总是可以输出文本,而不是使用断点,如果你真的想通过它来工作,你可以做一个cin,让程序等待你,在使用cin后它无法执行,这可能是Raistmaj的答案所暗示的。基本上,任何可能出现错误的地方,如果你看到一个断点,但不是下一个,你知道错误在它们之间的某个地方,我已经尝试过修改它,现在它确实运行了,但它看起来像是继续加载而不是结果。我认为itr问题是无效的,因为在执行explore()函数后,allstep向量不再为空
void explore(step currentstep, char Maze[][12],vector<step>& allsteps)
itr=allsteps.begin();
void explore(step currentstep, char Maze[][12],vector<step>& allsteps){
void explore(step currentstep, char Maze[][12],vector<step> allsteps){