GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Apr-2006, 17:37
amarufox amarufox is offline
New Member
 
Join Date: Apr 2006
Posts: 2
amarufox is on a distinguished road

Problem with getting multiple solutions from Knight's Tour...


Hi guys, I've been working on this for a while and I can't seem to figure out quite what I'm doing wrong here. So far this code successfully finds a knight's tour, but I can't seem to get it to backtrack and display multiple solutions for the problem (the number of solutions is given as user input). I'm not quite sure what direction to go in from here, most of my attempts haven't worked too well ;-) Thanks in advance for your help!

Here's the function code (I'm 99% sure the problem lies within this function, as the other things seem to work fine):

CPP / C++ / C Code:
bool knight(int board[][size], int size, int row, int column, int moves, int &num_solutions)
{


    if (num_solutions <= 0)
        exit(0);
    if(!is_it_a_valid_move(board, size, row, column))
        return false;

    board[row][column] = moves;

    if (moves == (size * size))
    {
        print_solution(board, size);
		num_solutions--;
		if (num_solutions <= 0)
			exit(0);		
		return true;
    }

	
    else
    {
        if (knight(board, size, row+2, column+1, moves+1, num_solutions))
            return true;
        else if (knight(board, size, row+2, column-1, moves+1, num_solutions))
            return true;
        else if (knight(board, size, row-2, column+1, moves+1, num_solutions))
            return true;
        else if (knight(board, size, row-2, column-1, moves+1, num_solutions))
            return true;
        else if (knight(board, size, row+1, column+2, moves+1, num_solutions))
            return true;
        else if (knight(board, size, row+1, column-2, moves+1, num_solutions))
            return true;
        else if (knight(board, size, row-1, column+2, moves+1, num_solutions))
            return true;
        else if (knight(board, size, row-1, column-2, moves+1, num_solutions))
            return true;
        else
        {
            board[row][column] = 0;
	     return false;
        }
	}

}

  #2  
Old 21-Apr-2006, 20:31
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Problem with getting multiple solutions from Knight's Tour...


Can you set a break point, to step through this function to see what's happening with either the logic and/or variable values? Or maybe add some prints at expected points. It's not fully clear what else may be happening in the program.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 22-Apr-2006, 08:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem with getting multiple solutions from Knight's Tour...


Quote:
Originally Posted by amarufox
I can't seem to get it to backtrack
I don't see any backtracking here.

At each step: when you find a solution from that position you don't look for any more. Don't you, somehow, have to test the other move directions from this position before returning? Otherwise how could you detect multiple solutions from any given position?
Quote:
Originally Posted by amarufox
Here's the function code (I'm 99% sure the problem lies within this function, as the other things seem to work fine):

Well, I'm 99% sure that the hidden surveillance camera didn't see me doing something that I shouldn't have done, but I'm getting out of town, just in case...


Regards,

Dave
  #4  
Old 22-Apr-2006, 13:08
amarufox amarufox is offline
New Member
 
Join Date: Apr 2006
Posts: 2
amarufox is on a distinguished road

Re: Problem with getting multiple solutions from Knight's Tour...


Thanks for the responses, guys. I got it figured out last night after a bit more walking through the code...you're right on, dave. I was returning too quickly, and not giving the recursive function time to finish going through the other alternatives (because the function would return after finding just one solution). Thanks!
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Knight's Tour - now with heuristics earachefl C++ Forum 0 09-Apr-2006 10:19
Knights Tour - Reloaded . kobi_hikri C Programming Language 12 03-Oct-2005 12:15
Linker errors with multiple file progam nkhambal C Programming Language 2 24-Apr-2005 02:37
knight tour (chess program) kai85 C++ Forum 10 25-Mar-2005 06:12
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:42.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.