2011-01-12 50 views
1
#include <iostream> 
using namespace std; 

#include <list> 


//A queue for the working set 
//x,y co-ords of the square, path length so far 
struct square { 
    int x; 
    int y; 
    int path_length; 
}; 

list<square> workingset; 


//A 2D array of ints to represent the board (duplicates list) 
int board[10][10]; 

void generatelegalmove(square node, int x_offset, int y_offset); 
void printboard(); 

void main() 
{ 
    //Initialises the board 
    int i, j; 
    for (i=0; i<10; i++) 
    { 
     for (j=0; j<10; j++) 
     { 
      board[i][j] = 0; 
     } 
    } 

    //The goal position - a number we will never reach 
    board[8][8] = 1337; 

    bool goal_found = false; 

    //Sets up initial position 
    square temp = {3, 7, 1}; 

    //Put initial position in working set and duplicates list 
    workingset.push_back(temp); 
    board[3][7] = 1; 

    //Loop (until a goal is found) 
    while(!goal_found) 
    { 
     //Get the head node from the working set 
     square nodetocheck = workingset.front(); 

     //Exit if the goal has been found 
     if(board[nodetocheck.x][nodetocheck.y] == 1337) 
     { 
      goal_found = true; 
      break; 
     } 

     //Generate the legal moves 
     generatelegalmove(nodetocheck, -1, 0); //One square to the left 
     generatelegalmove(nodetocheck, 0, -1); //One square up 
     generatelegalmove(nodetocheck, 1, 0); //One square to the right 
     generatelegalmove(nodetocheck, 0, 1); //One square down 


     if(!workingset.empty()) 
     { 
       //workingset.pop_front(); 
     } 

    //End Loop 
    } 

    //Print the Board 
    printboard(); 
    while(true); 

    //Trace back and print Trace back (once implemented) 

    //Print other info 

} 


void generatelegalmove(square node, int x_offset, int y_offset) 
{ 
    node.x = node.x + x_offset; 
    node.y = node.y + y_offset; 
    node.path_length = node.path_length+1; 
    //Is this square on the board 
    if((node.x >= 0) && 
     (node.x < 10) && 
     (node.y >= 0) && 
     (node.y < 10) && 
     //Is this square empty 
     (board[node.x][node.y] == 0)) 
    { 

     workingset.push_back(node); 
     board[node.x][node.y] = node.path_length; 
     //Add to working set 
     //Add to duplicates list 
    } 
    //(If a graphical animation is added, do it here, by printing the new board after each one, then sleeping for a few seconds) 
} 

我得到运行时错误'list iterator not dereferenable'。'list iterator not derefereenable'

我假设这是与在while循环内被调用的workingset.pop_front()一起做的,但我不知道我该怎么做才能解决这个问题。

每个循环,我想从列表的前面得到节点,使用它一点,然后从列表中删除该节点。

这是generatelegalmove()的代码 - 正如你所看到的,如果新的正方形在板上(即在数组的两个维度的范围内,并且正方形是空的)将这个新节点添加到工作集和板[] [](实际上是有效的复制列表)

+0

这将是值得添加所使用的变量工作集的声明。 – harper 2011-01-12 11:50:32

+0

您发布的哪一行代码会导致此错误?它是编译器错误还是运行时错误(调试检查)? – 2011-01-12 11:51:28

回答

2

鉴于您提供的示例,我可以看到一件事情是错误的。在每次循环迭代结束时,都会弹出前端节点。但是,你唯一的出口出你的循环,如果goal_found是真实的,这意味着该行:

square nodetocheck = workingset.front(); 

...很可能进入一个空的工作集。很明显,您可以调用其他可能添加节点的函数,但如果没有节点存在,这可能会成为问题。

编辑:我相信你的代码没有添加另一个节点,因为你使用的是按位运算符&而不是逻辑运算符&&,导致你的工作集不能获得节点。

相关问题