October 10th, 2013, 10:24 AM

Recursive function finding the smallest solution
I am doing a rush hour game and have built a recursive function to find out how many moves it would take to solve it. I am trying to get the smallest/best solution and am stumped as to how to continue. Any help would be appreciated.
Code:
void solveIt(int & moves_so_far ){
if(!carMadeIt() && moves_so_far <= 10 ){
if(moveForward(0)){
moves_so_far++;
solveIt(moves_so_far);
}else{
// moves_so_far++;
for(int i = 0; i < numOfCars; i++){
if(moveForward(i)){
moves_so_far++;
solveIt(moves_so_far);
if(i > 0){
moveBack(i);
}
}else if(i > 0 && moveBack(i)){
moves_so_far++;
solveIt(moves_so_far);
moveForward(i);
//}
}
}
/*
bestSolution is not working,
it gives me 8 moves if i choose 2 cars,
which is your car your trying to free at 0,0
and another car at 0,2 which blocks it in. It should give 5 moves.
*/
if(bestSolution == 0)
{
bestSolution = moves_so_far;
}else if( moves_so_far < bestSolution)
{
bestSolution = moves_so_far;
}
}//end for loop
//return moves_so_far;
//}//end else
}//if moves_so_far
}
October 11th, 2013, 02:45 AM

I am trying to get the smallest/best solution and am stumped as to how to continue. Any help would be appreciated.
Any help for which specific trouble you want? You are not asking any question...
October 11th, 2013, 04:59 AM

I am getting the largest solution, how do i get the smallest solution? Thanks.