#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    34
    Rep Power
    2

    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
    	
    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    240
    Rep Power
    29
    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...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    34
    Rep Power
    2
    I am getting the largest solution, how do i get the smallest solution? Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo