I am trying to implement iterative deepening search in a 2d array (a maze).
static void Run_Ids(int x, int y)
{
int depth_limit = 0;
while(!cutoff)
{
out.println("Doing search at depth: " + depth_limit);
depthLimitedSearch(x, y, depth_limit);
depth_limit++;
}
}
And here's my Limited depth first search using a stack. For some reason, it goes back and forth between two cells. It doesn't expand like it should. I think its something wrong with my DFS algorithm here.
static void depthLimitedSearch(int x, int y, int depth){
Pair successor; //pair is the x, y co-ordinate
successor = starting(); //set the successor to starting cell
stack = new Stack<>();
int i = 0;
stack.push(successor);
while (!stack.isEmpty())
{
out.println("i level: " + i);
Pair parent = stack.peek(); //pop it here?
if (parent.x == Environment.goal[0] && parent.y == Environment.goal[1]){ //check to see if it is the goal
cutoff = true;
out.println("goal found ");
break;
}
if (i == depth){
//stack.pop(); //pop here?
break;
}
else{
Pair leftPos,rightPos,upPos,downPos;
leftPos = leftPosition(parent.x, parent.y);
rightPos = rightPosition(parent.x, parent.y);
upPos = upPosition(parent.x, parent.y);
downPos = downPosition(parent.x, parent.y);
if(Environment.isMovePossible(rightPos.x, rightPos.y))
//if it can go right
stack.push(rightPos);
if(Environment.isMovePossible(leftPos.x, leftPos.y))
// if it can go left
stack.push(leftPos);
if(Environment.isMovePossible(downPos.x, downPos.y))
//if it can go down
stack.push(downPos);
if(Environment.isMovePossible(upPos.x, upPos.y))
//if it can go up
stack.push(upPos);
stack.pop(); //pop here?
} //else
i++;
}//while
}
I don't have that much experience with stack, and i am confused as to where to push it and where to pop. if somebody in here can point me to the right direction, that would be great!
Aucun commentaire:
Enregistrer un commentaire