#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)
}
Я получаю ошибку времени выполнения «Итератор списка не разыменовывается».
Я предполагаю, что это связано с вызовом workingset.pop_front()
из цикла while, но я не уверенчто я должен сделать, чтобы исправить это.
В каждом цикле я хочу получить узел в начале списка, немного поработать с ним, а затем удалить этот узел из списка.
Это код для generatelegalmove () - как вы можете видеть, если новый квадрат находится на доске (то есть в диапазоне 0-9 в обоих измерениях массива, и квадрат пуст)он добавит этот новый узел в рабочий набор и на доску [] [] (которая фактически является действующим списком дубликатов)